MongoDB
 Computer >> コンピューター >  >> プログラミング >> MongoDB

MongoDBで一括挿入を実行しますか?


MongoDBでの一括挿入には、initializeUnorderedBulkOp()を使用します。ドキュメントを使用してコレクションを作成しましょう-

> var bulkInsertDoc = db.demo663.initializeUnorderedBulkOp();
> bulkInsertDoc.insert( { Name: "John",CountryName:"US"} );
> bulkInsertDoc.insert( { Name: "Chris",CountryName:"UK"} );
> bulkInsertDoc.insert( { Name: "David",CountryName:"AUS"} );
> bulkInsertDoc.execute();
BulkWriteResult({
   "writeErrors" : [ ],
   "writeConcernErrors" : [ ],
   "nInserted" : 3,
   "nUpserted" : 0,
   "nMatched" : 0,
   "nModified" : 0,
   "nRemoved" : 0,
   "upserted" : [ ]
})

find()メソッドを使用してコレクションからすべてのドキュメントを表示する-

> db.demo663.find();

これにより、次の出力が生成されます-

{ "_id" : ObjectId("5ea1b41424113ea5458c7d05"), "Name" : "John", "CountryName" : "US" }
{ "_id" : ObjectId("5ea1b41424113ea5458c7d06"), "Name" : "Chris", "CountryName" : "UK" }
{ "_id" : ObjectId("5ea1b41424113ea5458c7d07"), "Name" : "David", "CountryName" : "AUS" }

  1. MongoDB全文検索を実行します

    MongoDBでの全文検索には、$textを使用します。 $ textは、フィールドのコンテンツに対してテキスト検索を実行します。ドキュメントを使用してコレクションを作成しましょう- > db.demo654.createIndex({Name:"text"}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter"

  2. 一括操作で複数の更新を実行し、MongoDBの配列内の要素を更新します

    これには、initializeOrderedBulkOp()を使用します。コレクションの新しいBulk()オペレーションビルダーを初期化して返します。ビルダーは、MongoDBが一括で実行する書き込み操作の順序付きリストを作成します。 ドキュメントを使用してコレクションを作成しましょう- >db.demo550.insertOne({"Name":"Chris","details":[{"Marks":49,Result:"fail"},{"Marks":58,Resu