插入文档
该页面提供了MongoDB中插入操作的示例。
建立集合如果该集合当前不存在,则插入操作将创建该集合。
db.inventory.insertOne(
{ item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
)
db.inventory.find( { item: "canvas" } )
3.2版中的新功能
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" } }
])
db.inventory.find( {} )
如果该集合当前不存在,则插入操作将创建该集合。
在MongoDB中,存储在集合中的每个文档都需要一个唯一的**_id字段作为主键。 如果插入的文档省略_id字段,则MongoDB驱动程序会自动为_id字段生成ObjectId**。
另可参考:
译者:杨帅
校对:杨帅
最近更新 1yr ago