MongoDB上的insert()
、insertOne()
和insertMany()
方法有什么区别.在什么情况下我应该使用每一个?
我读了这些文件,但不清楚什么时候使用它们.
MongoDB上的insert()
、insertOne()
和insertMany()
方法有什么区别.在什么情况下我应该使用每一个?
我读了这些文件,但不清楚什么时候使用它们.
MongoDB上的insert()、insertOne()和insertMany()方法有什么区别
db.collection.insert()
如文档中所述,将一份或多份文档插入集合并返回
> var d = db.collection.insert({"b": 3})
> d
WriteResult({ "nInserted" : 1 })
> var d2 = db.collection.insert([{"b": 3}, {'c': 4}])
> d2
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
db.collection.insertOne()
如文档中所述,将文档插入集合并返回如下所示的文档:
> var document = db.collection.insertOne({"a": 3})
> document
{
"acknowledged" : true,
"insertedId" : ObjectId("571a218011a82a1d94c02333")
}
db.collection.insertMany()
将多个文档插入到集合中,并返回如下所示的文档:
> var res = db.collection.insertMany([{"b": 3}, {'c': 4}])
> res
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("571a22a911a82a1d94c02337"),
ObjectId("571a22a911a82a1d94c02338")
]
}
在什么情况下我应该使用每一个?
在主驱动程序中,insert()
方法已被弃用,因此您应该使用