插入单个文档

Mongodb 使用以下几种方法来插入文档 , Mongodb V5.0+ 使用 mongosh 客户端:

  • 插入单个文档
    db.collection.insertOne() 将单个 文档插入到集合中。

如果该集合当前不存在,则插入操作将创建该集合。

如果文档未指定_id字段,则将在插入之前 mongod 添加该字段并为文档_id分配唯一的字段 。

insertOne() 方法具有以下语法:

db.collection.insertOne(
   <document>,
   {
      writeConcern: <document>
   }
)

注意,不同版本下面的 ObjectId 可选参数有差异,以下基于为 V4.4 版本:

ObjectId(<hexadecimal>) 返回新的ObjectId值。12 字节的ObjectId 值包括:

  • 一个 4 字节时间戳值,表示 ObjectId 的创建,以 Unix 纪元以来的秒数为单位。

  • 每个进程生成一次的 5 字节随机值。该随机值对于机器和进程来说是唯一的。

  • 一个 3 字节递增计数器,初始化为随机值。

以下示例获取 objectId()。 如 ObjectId(“64e2fb51bbd8c3243e14f05c”) ,其中

  • 64e2fb51: 对应十进制 1692597073( “2023-8-21 13:51:13” )即 UTC ( “2023-08-21T05:51:13Z” )。
    0110 0100 = 64
    1110 0010 = e2
    1111 1011 = fb
    0101 0001 = 51

(十六进制)64e2fb51 = 6*16 + 4*16 + 14*16 + 2*16 + 15*16 + 11*16 + 5*16 + 1*16
= 1610612736 + 67108864 + 14680064 + 131072 + 61440 + 2816 + 80 + 1
= 1692597073 (十进制)

  • bbd8c3243e: 每个进程生成一次的 5 字节随机值。
  • 14f05c: 一个 3 字节递增计数器。
sit_rs1:PRIMARY> x = ObjectId(); print(x);  x.getTimestamp()
ObjectId("64e2fb51bbd8c3243e14f05c")
ISODate("2023-08-21T05:51:13Z")


sit_rs1:PRIMARY> x = ObjectId(); print(x);  x.getTimestamp()
ObjectId("64e2fb53bbd8c3243e14f05d")
ISODate("2023-08-21T05:51:15Z")


sit_rs1:PRIMARY> x = ObjectId(); print(x);  x.getTimestamp()
ObjectId("64e2fb53bbd8c3243e14f05e")
ISODate("2023-08-21T05:51:15Z")
 
sit_rs1:PRIMARY> x = ObjectId(); print(x);  x.getTimestamp()
ObjectId("64e2fb56bbd8c3243e14f05f")
ISODate("2023-08-21T05:51:18Z")

尽管对象ID值应该随着时间的推移而增加,它们不一定是单调的。

这是因为他们:

  • 仅包含一秒的时间分辨率,因此对象ID在同一秒内创建的值没有保证的顺序。

  • 由客户端生成,这些客户端可能具有不同的系统时钟。

以下示例将新文档插入到 order2 集合中。如果文档未指定_id字段,MongoDB 会将_id具有 ObjectId 值的字段添加到新文档中,如下:

sit_rs1:PRIMARY> db.order2.insertOne({ cust_id: "A", ord_date: new Date("2023-06-01"), price: 15, items: [ { sku: "apple", qty: 5, price: 2.5 }, { sku: "apples", qty: 5, price: 2.5 } ], status: "1" }); 
{
        "acknowledged" : true,
        "insertedId" : ObjectId("64e31b9bbbd8c3243e14f060")
}

sit_rs1:PRIMARY> db.order2.find()
{ "_id" : ObjectId("64e31b9bbbd8c3243e14f060"), "cust_id" : "A", "ord_date" : ISODate("2023-06-01T00:00:00Z"), "price" : 15, "items" : [ { "sku" : "apple", "qty" : 5, "price" : 2.5 }, { "sku" : "apples", "qty" : 5, "price" : 2.5 } ], "status" : "1" }

插入多个文档

db.collection.insertMany() 可以将多个 文档插入到集合中。将文档数组传递给该方法。

insertMany() 方法具有以下语法:

db.collection.insertMany(
   [ <document 1> , <document 2>, ... ],
   {
      writeConcern: <document>,
      ordered: <boolean>
   }
)

如果ordered设置为 false,文档将以无序格式插入,并且可以重新排序以mongod提高性能。如果使用无序,应用程序不应依赖于插入的顺序

在分片集合上执行ordered操作列表通常比执行 unordered列表慢,因为对于有序列表,每个操作都必须等待前一个操作完成。

排除Write Concern错误,有序操作在发生错误后停止 !!!!!!!!!,而无序操作继续处理队列中任何剩余的写操作。

以下示例将三个新文档插入到 order2 集合中。如果文档未指定 _id字段,MongoDB 会_id向每个文档添加带有 ObjectId 值的字段

sit_rs1:PRIMARY> db.order2.insertMany([   
...    { cust_id: "A", ord_date: new Date("2023-06-08"), price: 60, items: [ { sku: "apple", qty: 8, price: 2.5 }, { sku: "banana", qty: 5, price: 10 } ], status: "1" },
...    { cust_id: "B", ord_date: new Date("2023-06-08"), price: 55, items: [ { sku: "apple", qty: 10, price: 2.5 }, { sku: "pears", qty: 10, price: 2.5 } ], status: "1" },
...    { cust_id: "B", ord_date: new Date("2023-06-18"), price: 26, items: [ { sku: "apple", qty: 10, price: 2.5 } ], status: "1" },
...    { cust_id: "B", ord_date: new Date("2023-06-19"), price: 40, items: [ { sku: "banana", qty: 5, price: 10 } ], status: "1"},
...    { cust_id: "C", ord_date: new Date("2023-06-19"), price: 38, items: [ { sku: "carrots", qty: 10, price: 1.0 }, { sku: "apples", qty: 10, price: 2.5 } ], status: "1" },
...    { cust_id: "C", ord_date: new Date("2023-06-20"), price: 21, items: [ { sku: "apple", qty: 10, price: 2.5 } ], status: "1" },
...    { cust_id: "D", ord_date: new Date("2023-06-20"), price: 76, items: [ { sku: "banana", qty: 5, price: 10 }, { sku: "apples", qty: 10, price: 2.5 } ], status: "1" },
...    { cust_id: "D", ord_date: new Date("2023-06-20"), price: 51, items: [ { sku: "carrots", qty: 5, price: 1.0 }, { sku: "apples", qty: 10, price: 2.5 }, { sku: "apple", qty: 10, price: 2.5 } ], status: "1" },
...    { cust_id: "D", ord_date: new Date("2023-06-23"), price: 23, items: [ { sku: "apple", qty: 10, price: 2.5 } ], status: "1" }
... ])
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("64e31cb0bbd8c3243e14f061"),
                ObjectId("64e31cb0bbd8c3243e14f062"),
                ObjectId("64e31cb0bbd8c3243e14f063"),
                ObjectId("64e31cb0bbd8c3243e14f064"),
                ObjectId("64e31cb0bbd8c3243e14f065"),
                ObjectId("64e31cb0bbd8c3243e14f066"),
                ObjectId("64e31cb0bbd8c3243e14f067"),
                ObjectId("64e31cb0bbd8c3243e14f068"),
                ObjectId("64e31cb0bbd8c3243e14f069")
        ]
}

sit_rs1:PRIMARY> db.order2.find()
{ "_id" : ObjectId("64e31b9bbbd8c3243e14f060"), "cust_id" : "A", "ord_date" : ISODate("2023-06-01T00:00:00Z"), "price" : 15, "items" : [ { "sku" : "apple", "qty" : 5, "price" : 2.5 }, { "sku" : "apples", "qty" : 5, "price" : 2.5 } ], "status" : "1" }
{ "_id" : ObjectId("64e31cb0bbd8c3243e14f061"), "cust_id" : "A", "ord_date" : ISODate("2023-06-08T00:00:00Z"), "price" : 60, "items" : [ { "sku" : "apple", "qty" : 8, "price" : 2.5 }, { "sku" : "banana", "qty" : 5, "price" : 10 } ], "status" : "1" }
{ "_id" : ObjectId("64e31cb0bbd8c3243e14f062"), "cust_id" : "B", "ord_date" : ISODate("2023-06-08T00:00:00Z"), "price" : 55, "items" : [ { "sku" : "apple", "qty" : 10, "price" : 2.5 }, { "sku" : "pears", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : ObjectId("64e31cb0bbd8c3243e14f063"), "cust_id" : "B", "ord_date" : ISODate("2023-06-18T00:00:00Z"), "price" : 26, "items" : [ { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : ObjectId("64e31cb0bbd8c3243e14f064"), "cust_id" : "B", "ord_date" : ISODate("2023-06-19T00:00:00Z"), "price" : 40, "items" : [ { "sku" : "banana", "qty" : 5, "price" : 10 } ], "status" : "1" }
{ "_id" : ObjectId("64e31cb0bbd8c3243e14f065"), "cust_id" : "C", "ord_date" : ISODate("2023-06-19T00:00:00Z"), "price" : 38, "items" : [ { "sku" : "carrots", "qty" : 10, "price" : 1 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : ObjectId("64e31cb0bbd8c3243e14f066"), "cust_id" : "C", "ord_date" : ISODate("2023-06-20T00:00:00Z"), "price" : 21, "items" : [ { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : ObjectId("64e31cb0bbd8c3243e14f067"), "cust_id" : "D", "ord_date" : ISODate("2023-06-20T00:00:00Z"), "price" : 76, "items" : [ { "sku" : "banana", "qty" : 5, "price" : 10 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : ObjectId("64e31cb0bbd8c3243e14f068"), "cust_id" : "D", "ord_date" : ISODate("2023-06-20T00:00:00Z"), "price" : 51, "items" : [ { "sku" : "carrots", "qty" : 5, "price" : 1 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 }, { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : ObjectId("64e31cb0bbd8c3243e14f069"), "cust_id" : "D", "ord_date" : ISODate("2023-06-23T00:00:00Z"), "price" : 23, "items" : [ { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }

无序插入,以下尝试插入带有_id 字段 和 的多个文档 ordered: false。文档数组包含两个具有重复_id字段的文档。如下:

其中 “_id” : 2, “_id” : 6 主键冲突错误 (duplicate key error),其它文档插入成功 。

sit_rs1:PRIMARY> try {
...    db.order2.insertMany([
...    { _id: 1, cust_id: "A", ord_date: new Date("2023-06-01"), price: 15, items: [ { sku: "apple", qty: 5, price: 2.5 }, { sku: "apples", qty: 5, price: 2.5 } ], status: "1" },
...    { _id: 2, cust_id: "A", ord_date: new Date("2023-06-08"), price: 60, items: [ { sku: "apple", qty: 8, price: 2.5 }, { sku: "banana", qty: 5, price: 10 } ], status: "1" },
...    { _id: 2, cust_id: "B", ord_date: new Date("2023-06-08"), price: 55, items: [ { sku: "apple", qty: 10, price: 2.5 }, { sku: "pears", qty: 10, price: 2.5 } ], status: "1" },
...    { _id: 4, cust_id: "B", ord_date: new Date("2023-06-18"), price: 26, items: [ { sku: "apple", qty: 10, price: 2.5 } ], status: "1" },
...    { _id: 5, cust_id: "B", ord_date: new Date("2023-06-19"), price: 40, items: [ { sku: "banana", qty: 5, price: 10 } ], status: "1"},
...    { _id: 6, cust_id: "C", ord_date: new Date("2023-06-19"), price: 38, items: [ { sku: "carrots", qty: 10, price: 1.0 }, { sku: "apples", qty: 10, price: 2.5 } ], status: "1" },
...    { _id: 6, cust_id: "C", ord_date: new Date("2023-06-20"), price: 21, items: [ { sku: "apple", qty: 10, price: 2.5 } ], status: "1" },
...    { _id: 8, cust_id: "D", ord_date: new Date("2023-06-20"), price: 76, items: [ { sku: "banana", qty: 5, price: 10 }, { sku: "apples", qty: 10, price: 2.5 } ], status: "1" },
...    { _id: 9, cust_id: "D", ord_date: new Date("2023-06-20"), price: 51, items: [ { sku: "carrots", qty: 5, price: 1.0 }, { sku: "apples", qty: 10, price: 2.5 }, { sku: "apple", qty: 10, price: 2.5 } ], status: "1" },
...    { _id: 10, cust_id: "D", ord_date: new Date("2023-06-23"), price: 23, items: [ { sku: "apple", qty: 10, price: 2.5 } ], status: "1" }
... ], { ordered: false });
... } catch (e) {
...    print (e);
... }
BulkWriteError({
        "writeErrors" : [
                {
                        "index" : 2,
                        "code" : 11000,
                        "errmsg" : "E11000 duplicate key error collection: test.order2 index: _id_ dup key: { _id: 2.0 }",
                        "op" : {
                                "_id" : 2,
                                "cust_id" : "B",
                                "ord_date" : ISODate("2023-06-08T00:00:00Z"),
                                "price" : 55,
                                "items" : [
                                        {
                                                "sku" : "apple",
                                                "qty" : 10,
                                                "price" : 2.5
                                        },
                                        {
                                                "sku" : "pears",
                                                "qty" : 10,
                                                "price" : 2.5
                                        }
                                ],
                                "status" : "1"
                        }
                },
                {
                        "index" : 6,
                        "code" : 11000,
                        "errmsg" : "E11000 duplicate key error collection: test.order2 index: _id_ dup key: { _id: 6.0 }",
                        "op" : {
                                "_id" : 6,
                                "cust_id" : "C",
                                "ord_date" : ISODate("2023-06-20T00:00:00Z"),
                                "price" : 21,
                                "items" : [
                                        {
                                                "sku" : "apple",
                                                "qty" : 10,
                                                "price" : 2.5
                                        }
                                ],
                                "status" : "1"
                        }
                }
        ],
        "writeConcernErrors" : [ ],
        "nInserted" : 8,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})

sit_rs1:PRIMARY> db.order2.find()
{ "_id" : 1, "cust_id" : "A", "ord_date" : ISODate("2023-06-01T00:00:00Z"), "price" : 15, "items" : [ { "sku" : "apple", "qty" : 5, "price" : 2.5 }, { "sku" : "apples", "qty" : 5, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 2, "cust_id" : "A", "ord_date" : ISODate("2023-06-08T00:00:00Z"), "price" : 60, "items" : [ { "sku" : "apple", "qty" : 8, "price" : 2.5 }, { "sku" : "banana", "qty" : 5, "price" : 10 } ], "status" : "1" }
{ "_id" : 4, "cust_id" : "B", "ord_date" : ISODate("2023-06-18T00:00:00Z"), "price" : 26, "items" : [ { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 5, "cust_id" : "B", "ord_date" : ISODate("2023-06-19T00:00:00Z"), "price" : 40, "items" : [ { "sku" : "banana", "qty" : 5, "price" : 10 } ], "status" : "1" }
{ "_id" : 6, "cust_id" : "C", "ord_date" : ISODate("2023-06-19T00:00:00Z"), "price" : 38, "items" : [ { "sku" : "carrots", "qty" : 10, "price" : 1 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 8, "cust_id" : "D", "ord_date" : ISODate("2023-06-20T00:00:00Z"), "price" : 76, "items" : [ { "sku" : "banana", "qty" : 5, "price" : 10 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 9, "cust_id" : "D", "ord_date" : ISODate("2023-06-20T00:00:00Z"), "price" : 51, "items" : [ { "sku" : "carrots", "qty" : 5, "price" : 1 }, { "sku" : "apples", "qty" : 10, "price" : 2.5 }, { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }
{ "_id" : 10, "cust_id" : "D", "ord_date" : ISODate("2023-06-23T00:00:00Z"), "price" : 23, "items" : [ { "sku" : "apple", "qty" : 10, "price" : 2.5 } ], "status" : "1" }

插入的附加方法

以下方法还可以将新文档添加到集合中:

db.collection.updateOne() 与选项一起使用时upsert: true。

db.collection.updateMany() 与选项一起使用时upsert: true。

db.collection.findAndModify() 与选项一起使用时upsert: true。

db.collection.findOneAndUpdate() 与选项一起使用时 upsert: true。

db.collection.findOneAndReplace() 与选项一起使用时 upsert: true。

db.collection.bulkWrite().

以上方法可以参考 以下的文章 :

(1)Mongodb 更新集合的方法到底有几种 (上) ?
(2)Mongodb 更新集合的方法到底有几种 (中) ?
(3)Mongodb 更新集合的方法到底有几种 (下) ?

08-22 15:12