在MongoDB中插入文档的5种方法

发布于

MongoDB提供了许多将文档插入集合的方法。

以下是使用 mongo shell 时将文档插入集合的 5 种方法。

方法insertOne()

该方法将单个文档插入到集合中。[insertOne()](https://database.guide/mongodb-insertone/ "MongoDB insertOne()")

它的全名是 ,其中 是要将文档插入到的集合的名称。db.collection.insertOne()``collection

下面是将文档插入名为 的集合的示例:pets

db.pets.insertOne( { 
    name: "Scratch", 
    type: "Cat" 
    } )

结果:

{ “acknowledged” : true, “insertedId” : ObjectId(“5fe2d15637b49e0faf1af214”) }

该方法返回一个包含以下内容的文档:db.collection.insertOne()

  • 一个布尔值,就好像操作在关注写入的情况下运行,或者如果写入关注被禁用。acknowledged``true``false
  • 具有插入文档值的域。insertedId``_id

方法insertMany()

该方法与 类似,不同之处在于它将多个文档插入到集合中。[insertMany()](https://database.guide/mongodb-insertmany/ "MongoDB insertMany()")``insertOne()

同样与 类似的方式是,它的全名是 ,其中 是要将文档插入到的集合的名称。这适用于本文中列出的所有方法。insertOne()``db.collection.insertMany()``collection

下面是用于将多个文档插入名为 的集合的示例:db.collection.insertMany()``pets

db.pets.insertMany([
    { _id: 1, name: "Wag", type: "Dog" },
    { _id: 2, name: "Bark", type: "Dog" },
    { _id: 3, name: "Meow", type: "Cat" }
    ])

结果:

{ “acknowledged” : true, “insertedIds” : [ 1, 2, 3 ] }

该方法返回一个包含以下内容的文档:db.collection.insertMany()

  • 一个布尔值,就好像操作在关注写入的情况下运行,或者如果写入关注被禁用。acknowledged``true``false
  • 每个成功插入的文档的值数组。_id

方法insert()

该方法类似于 和 的组合。它使您能够将单个文档或多个文档插入到集合中。[insert()](https://database.guide/mongodb-insert/ "MongoDB insert()")``insertOne()``insertMany()

下面是用于插入单个文档的示例:db.collection.insert()

db.pets.insert(
    { name: "Bruce", type: "Bat" }
    )

结果:

WriteResult({ “nInserted” : 1 })

插入单个文档时,返回一个对象。插入文档数组时,它将返回一个对象。db.collection.insert()``WriteResult``BulkWriteResult

下面是用于插入多个文档的示例。db.collection.insert()

db.pets.insert([
    { _id: 1, name: "Wag", type: "Dog" },
    { _id: 2, name: "Bark", type: "Dog" },
    { _id: 3, name: "Meow", type: "Cat" }
    ])

结果:

BulkWriteResult({ “writeErrors” : [ ], “writeConcernErrors” : [ ], “nInserted” : 3, “nUpserted” : 0, “nMatched” : 0, “nModified” : 0, “nRemoved” : 0, “upserted” : [ ] })

如前所述,插入文档数组时会返回一个对象。BulkWriteResult

方法bulkWrite()

该方法使您能够执行批量写入操作。[bulkWrite()](https://database.guide/mongodb-bulkwrite/ "MongoDB bulkWrite()")

实际上,已经执行了批量插入操作。与插入文档数组时相同。但使您能够通过单个方法调用执行批量插入、更新和删除操作。insertMany()``insert()``bulkWrite()

例:

db.pets.bulkWrite([
    { insertOne: { "document": { "_id": 1, "name": "Bubbles", "type": "Fish" }}},
    { insertOne: { "document": { "_id": 2, "name": "Wag", "type": "Dog" }}},
    { updateOne : {
        "filter" : { "_id" : 3 },
        "update" : { $set : { "name" : "Fluffy", "type" : "Cat" } },
        "upsert" : true
    } },
    { replaceOne : {
        "filter" : { "_id" : 4 },
        "replacement" : { "name" : "Bite", "type" : "Dog", "weight": 5 },
        "upsert" : true
    } }
])

结果:

{ “acknowledged” : true, “deletedCount” : 0, “insertedCount” : 2, “matchedCount” : 0, “upsertedCount” : 2, “insertedIds” : { “0” : 1, “1” : 2 }, “upsertedIds” : { “2” : 3, “3” : 4 } }

在本例中,集合实际上并不存在(我在前面的示例之后删除了它),因此集合中现在唯一的文档是此示例中指定的文档。

让我们来看看这个系列。

db.pets.find()

结果:

{ “_id” : 1, “name” : “Bubbles”, “type” : “Fish” } { “_id” : 2, “name” : “Wag”, “type” : “Dog” } { “_id” : 3, “name” : “Fluffy”, “type” : “Cat” } { “_id” : 4, “name” : “Bite”, “type” : “Dog”, “weight” : 5 }

正如预期的那样,所有四个文件都已插入。

更新插入操作

这就引出了在MongoDB中将文档插入集合的第五个方法 - upsert操作。

这更像是插入文档的条件方法。更新插入是可用于更新操作的选项。仅当指定的文档尚不存在时,它才会插入新文档。如果存在,则更新原始文档(并且不插入任何文档)。

我们已经看到了更新插入的例子。在前面的示例中,我们在执行 和 操作时指定了。这导致插入了两个文档,因为集合不包含与其筛选条件匹配的文档。"upsert" : true``updateOne``replaceOne

这是更新器的另一个示例。这一次,我们将在方法上使用它。updateOne()

db.pets.updateOne( 
    { name: "Harry" },
    { $set: { type: "Hamster" } },
    { upsert: true }
    )

结果:

{ “acknowledged” : true, “matchedCount” : 0, “modifiedCount” : 0, “upsertedId” : ObjectId(“5fe3dc44d991410169410524”) }

在这里,我们想把所有叫哈利的宠物都设定成仓鼠。但是没有叫哈利的宠物,所以进行了一次升级插入。

以下方法接受该参数:upsert

因此,如果指定,则这些方法在尝试更新不存在的文档时将执行更新插入操作。upsert: true