web-dev-qa-db-ja.com

既存のmongodbでの単一のコレクションの復元

単一のコレクションを既存のデータベースに復元できないという悲惨な失敗をしています。 mongoバージョン2.6.7でUbuntu 14.04を実行しています。ホームディレクトリに基づいてdump/mydbname/contents.bsonがあります。

走ったら

mongorestore --collection contents --db mydbname

その後、私は得る:

connected to: 127.0.0.1
don't know what to do with file [dump]

パスに追加した場合

mongorestore --collection contents --db mydbname --dbpath dump/mydbname

その後、私は得る

If you are running a mongod on the same path you should connect to that instead of direct data file access

他のさまざまな組み合わせやオプションなどを試してみましたが、困惑することはないので、助けを求めてコミュニティに来ています!

31
JonRed

単一のコレクションを復元する場合は、コレクションのダンプファイルを指定する必要があります。コレクションのダンプファイルは、「dump/dbname /」フォルダーにあります。したがって、ダンプフォルダーが現在の作業ディレクトリにあると仮定すると、コマンドは次のようになります-

mongorestore --db mydbname --collection mycollection dump/mydbname/mycollection.bson
68
Abhay PS

これは--nsIncludeオプション:

mongorestore --nsInclude test.purchaseorders dump/

dump/はmongodumpデータを含むフォルダー、testはdb、purchaseordersはコレクションです。

https://docs.mongodb.com/manual/reference/program/mongorestore/

0
wordsforthewise

Mongodbで特定のコレクションを復元する手順。

1)ダンプフォルダーが存在するディレクトリに移動します。

2)データベース名とコレクション名に従って変更して、次のコマンドを実行します。

mongorestore --db mydbname --collection mycollection dump/mydbname/mycollection.bson

- あなたが取得する場合 Failed: yourdbname.collection.name: error creating indexes for collection.name: createIndex error: The field 'safe' is not valid for an index specification error、次のコマンドを使用できます:

mongorestore --db mydbname --collection mycollection dump/mydbname/mycollection.bson --noIndexRestore
0

複数のコレクションを復元する場合、ループを使用できます。

for file in "$HOME/mongodump/dev/<your-db>/"* ; do

  if [[ "$file" != "*metadata*" && "$file" != "system.*" && "$file" != "locks.*" ]]; then

    file="$(basename "$file”)"

    mongorestore \
        --db cdt_dev \
        --collection "${file%.*}" \   # filename w/o extension
        --Host "<your-Host>" \
        --authenticationDatabase "<your-auth-db>" \
        -u "user" \
        -p "pwd" \
        "$HOME/mongodump/dev/<your-db>/$file"

  fi;

done
0
Alexander Mills