让我们创建一个包含文档的集合-
> db.demo227.insertOne({"_id":"101","Name":"Chris"}); { "acknowledged" : true, "insertedId" : "101" } > db.demo227.insertOne({"_id":"102","Name":"Bob"}); { "acknowledged" : true, "insertedId" : "102" }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo227.find();
这将产生以下输出-
{ "_id" : "101", "Name" : "Chris" } { "_id" : "102", "Name" : "Bob" }
以下是从键值对列表中更新一组文档的查询-
> var bulkUpdateValue = [{"_id": "101", "Name": "Robert"}, ... {"_id": "102", "Name": "Sam"} ...]; > var bulkUpdate = db.demo227.initializeUnorderedBulkOp(); > var updateCounter= undefined; > for (var i = 0; i < bulkUpdateValue.length; i++){ ... updateCounter = bulkUpdateValue[i]; ... bulkUpdate.find( {_id: updateCounter._id} ).update( {$set: {Name: updateCounter.Name}} ); ... } > bulkUpdate.execute(); BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 0, "nUpserted" : 0, "nMatched" : 2, "nModified" : 2, "nRemoved" : 0, "upserted" : [ ] })
在find()
方法的帮助下显示集合中的所有文档-
> db.demo227.find();
这将产生以下输出-
{ "_id" : "101", "Name" : "Robert" } { "_id" : "102", "Name" : "Sam" }