insertOne()插入一个文档
insertone()将单个文档插入到集合中。
db.collection.insertOne( <document>, { writeConcern: <document> } )
下面的示例将一个新文档插入到inventory集合中。如果文档没有指定_id字段,MongoDB将带有ObjectId值的_id字段添加到新文档中。看到插入行为。
db.inventory.insertOne( { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } } )
insertOne()返回一个文档,其中包含新插入的文档的_id字段值。有关返回文档的示例,请参见db.collection.insertOne()引用。
要检索刚刚插入的文档,查询集合:
db.inventory.find( { item: "canvas" } )
在下面的示例中,传递给insertOne()方法的文档包含_id字段。_id的值在集合中必须是唯一的,以避免重复键错误。
try { db.products.insertOne( { _id: 10, item: "box", qty: 20 } ); } catch (e) { print (e); }
成功插入将返回:
{ "acknowledged" : true, "insertedId" : 10 }
如果_id重复,将返回:
riteError({ "index" : 0, "code" : 11000, "errmsg" : "E11000 duplicate key error collection: inventory.products index: _id_ dup key: { : 10.0 }", "op" : { "_id" : 10, "item" : "packing peanuts", "qty" : 200 } })
insertMany()插入多个文档
insertmany()可以在一个集合中插入多个文档。将文档数组传递给该方法。
db.collection.insertMany( [ <document 1> , <document 2>, ... ], { writeConcern: <document>, ordered: <boolean> } )
下面的示例将三个新文档插入到inventory集合中。如果文档没有指定_id字段,MongoDB会向每个文档添加带有ObjectId值的_id字段。
db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
{ item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
{ item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
])
insertMany()返回一个文档,其中包含新插入的文档_id字段值。
要检索插入的文档,查询集合:
db.inventory.find( {} )
插入时不指定_id:
try { db.products.insertMany( [ { item: "card", qty: 15 }, { item: "envelope", qty: 20 }, { item: "stamps" , qty: 30 } ] ); } catch (e) { print (e); }
返回:
{ "acknowledged" : true, "insertedIds" : [ ObjectId("562a94d381cb9f1cd6eb0e1a"), ObjectId("562a94d381cb9f1cd6eb0e1b"), ObjectId("562a94d381cb9f1cd6eb0e1c") ] }
插入时指定_id:
try { db.products.insertMany( [ { _id: 10, item: "large box", qty: 20 }, { _id: 11, item: "small box", qty: 55 }, { _id: 12, item: "medium box", qty: 30 } ] ); } catch (e) { print (e); }
若成功返回:
{ "acknowledged" : true, "insertedIds" : [ 10, 11, 12 ] }
若失败返回:
BulkWriteError({
"writeErrors" : [
{
"index" : 0,
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: test.products index: _id_ dup key: { : 10.0 }",
"op" : {
"_id" : 10,
"item" : "large box",
"qty" : 20
}
}
],
"writeConcernErrors" : [ ],
"nInserted" : 0,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
insert()插入单个文档或多个文档
writeConcern:可选的。表示写关注的文档。忽略使用默认的写关注点。看到写问题。
如果在事务中运行,不要显式设置操作的写关注。要在事务中使用写关注点,请参见事务选项(读关注点/写关注点/读首选项)。
orderd: 可选的。如果为真,执行数组中文档的有序插入,如果其中一个文档发生错误,MongoDB将返回,而不处理数组中的其余文档。
如果为false,则执行无序插入,如果其中一个文档出现错误,则继续处理数组中的其余文档。
默认值为true。
db.products.insert( { item: "card", qty: 15 } )
db.products.insert( { _id: 10, item: "box", qty: 20 } )
db.products.insert( [ { _id: 11, item: "pencil", qty: 50, type: "no.2" }, { item: "pen", qty: 20 }, { item: "eraser", qty: 25 } ] )
db.products.insert( [ { _id: 20, item: "lamp", qty: 50, type: "desk" }, { _id: 21, item: "lamp", qty: 20, type: "floor" }, { _id: 22, item: "bulk", qty: 100 } ], { ordered: false } )
db.collection.save()¶
根据文档参数更新现有文档或插入新文档。
使用格式:
db.collection.save( <document>, { writeConcern: <document> } )
Returns: |
A WriteResult object that contains the status of the operation. |
如果文档不包含_id字段,则save()方法调用insert()方法。在操作期间,mongo shell将创建一个ObjectId并将其分配给_id字段。
如果文档包含一个_id字段,那么save()方法相当于一个update, upsert选项设置为true,查询谓词设置为_id字段。
save操作和insert操作区别
在于当遇到_id相同的情况下:
save完成保存操作并覆盖之前数据,insert则会报错.
save不支持批操作