创建文档
| db.collection.insertOne() | 插入单个文档到集合中 |
|---|---|
| db.collection.insertMany() | 插入多个文档到集合中 |
| db.collection.insert() | 将1个或多个文档插入到集合中 |
插入单个文档
db.products.insertOne( { item: "card", qty: 15 } );
插入多个文档
db.products.insertMany([
{ item: "card1", qty: 15 },
{ item: "card2", qty: 16 }
{ item: "card3", qty: 17 }
])
插入行为
1、集合创建
如果该集合当前不存在,则插入操作将创建该集合。
2、_id 字段
在 MongoDB 中,存储在集合中的每个文档都需要一个唯一的 _id 字段作为主键。如果插入的文档省略 _id 字段,则 MongoDB 驱动程序会自动为 _id 字段生成 ObjectId。\
若有收获,就点个赞吧
本文正在参加「技术专题19期 漫谈数据库技术」活动