对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

Bulk.find。 更新或插入(upsert)()(mongosh方法)

提示

MongoDB 还提供了 db.collection.bulkWrite() 方法用于执行批量写入操作。

Bulk.find.upsert()

将更新或替换操作的 upsert 选项设置为 true,事务语法如下:

Bulk.find(<query>).upsert().update(<update>);
Bulk.find(<query>).upsert().updateOne(<update>);
Bulk.find(<query>).upsert().replaceOne(<replacement>);

upsert 选项设置为 true 时,如果不存在 Bulk.find() 条件的匹配文档,则更新或替换操作将执行插入。如果匹配的文档确实存在,那么更新或替换操作就会执行指定的更新或替换。

Use Bulk.find.upsert() with the following write operations:

此命令可用于以下环境中托管的部署:

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

注意

所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令

The following describe the insert behavior of various write operations when used in conjunction with Bulk.find.upsert().

Bulk.find.replaceOne() 方法接受仅包含字段和值对的替换文档作为其参数:

var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { item: "abc123" } ).upsert().replaceOne(
{
item: "abc123",
status: "P",
points: 100,
}
);
bulk.execute();

If the replacement operation with the Bulk.find.upsert() option performs an insert, the inserted document is the replacement document. If neither the replacement document nor the query document specifies an _id field, MongoDB adds the _id field:

{
"_id" : ObjectId("52ded3b398ca567f5c97ac9e"),
"item" : "abc123",
"status" : "P",
"points" : 100
}

Bulk.find.updateOne() 方法接受以下任一项作为其参数

如果 参数 是仅包含字段和值对的替换文档:

var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { status: "P" } ).upsert().updateOne(
{
item: "TBD",
points: 0,
inStock: true,
status: "I"
}
);
bulk.execute();

Then, if the update operation with the Bulk.find.upsert() option performs an insert, the inserted document is the replacement document. If neither the replacement document nor the query document specifies an _id field, MongoDB adds the _id field:

{
"_id" : ObjectId("52ded5a898ca567f5c97ac9f"),
"item" : "TBD",
"points" : 0,
"inStock" : true,
"status" : "I"
}

如果参数是一份仅包含更新操作符表达式的更新文档:

var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { status: "P", item: null } ).upsert().updateOne(
{
$setOnInsert: { qty: 0, inStock: true },
$set: { points: 0 }
}
);
bulk.execute();

Then, if the update operation with the Bulk.find.upsert() option performs an insert, the update operation inserts a document with field and values from the query document of the Bulk.find() method and then applies the specified updates from the update document. If neither the update document nor the query document specifies an _id field, MongoDB adds the _id field:

{
"_id" : ObjectId("5e28d1a1500153bc2872dadd"),
"item" : null,
"status" : "P",
"inStock" : true,
"points" : 0,
"qty" : 0
}

更新方法可以接受聚合管道。例如,以下使用:

  • $replaceRoot 阶段,它提供与 $setOnInsert 更新操作符表达式略微相似的行为,

  • $set 阶段,可以提供与 $set 更新操作符表达式类似的行为,

  • 聚合变量 NOW 解析为当前日期时间,可以提供与 $currentDate 更新操作符表达式类似的行为。

var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { item: "Not Found", status: "P" } ).upsert().updateOne(
[
{ $replaceRoot: { newRoot: { $mergeObjects: [ { qty: 0, inStock: true }, "$$ROOT" ] } } },
{ $set: { points: 0, lastModified: "$$NOW" } }
]
);
bulk.execute();

Then, if the update operation with the Bulk.find.upsert() option performs an insert, the update operation inserts a document with field and values from the query document of the Bulk.find() method and then applies the specified aggregation pipeline. If neither the update document nor the query document specifies an _id field, MongoDB adds the _id field:

{
"_id" : ObjectId("5e28cf1e500153bc2872d49f"),
"qty" : 0,
"inStock" : true,
"item" : "Not Found",
"status" : "P",
"points" : 0,
"lastModified" : ISODate("2020-01-22T22:39:26.789Z")
}

When using upsert() with the multiple document update method Bulk.find.update(), if no documents match the query condition, the update operation inserts a single document.

Bulk.find.update() 方法的参数可以是:

如果参数是一份仅包含更新操作符表达式的更新文档:

var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { status: "P" } ).upsert().update(
{
$setOnInsert: { qty: 0, inStock: true },
$set: { status: "I", points: "0" }
}
);
bulk.execute();

Then, if the update operation with the Bulk.find.upsert() option performs an insert, the update operation inserts a single document with the fields and values from the query document of the Bulk.find() method and then applies the specified update from the update document. If neither the update document nor the query document specifies an _id field, MongoDB adds the _id field:

{
"_id": ObjectId("52ded81a98ca567f5c97aca1"),
"status": "I",
"qty": 0,
"inStock": true,
"points": "0"
}

更新方法可以接受聚合管道。例如,以下使用:

  • $replaceRoot 阶段,它提供与 $setOnInsert 更新操作符表达式略微相似的行为,

  • $set 阶段,可以提供与 $set 更新操作符表达式类似的行为,

  • 聚合变量 NOW,解析为当前日期时间,可以提供与 $currentDate 更新操作符表达式相似的行为。NOW 的值在整个管道中保持不变。要访问聚合变量,在变量前加上双美元符号 $$ 并用引号引起来。

var bulk = db.items.initializeUnorderedBulkOp();
bulk.find( { item: "New Item", status: "P" } ).upsert().update(
[
{ $replaceRoot: { newRoot: { $mergeObjects: [ { qty: 0, inStock: true }, "$$ROOT" ] } } },
{ $set: { points: 0, lastModified: "$$NOW" } }
]
);
bulk.execute();

Then, if the update operation with the Bulk.find.upsert() option performs an insert, the update operation inserts a single document with the fields and values from the query document of the Bulk.find() method and then applies the aggregation pipeline. If neither the update document nor the query document specifies an _id field, MongoDB adds the _id field:

{
"_id" : ObjectId("5e2920a5b4c550aad59d18a1"),
"qty" : 0,
"inStock" : true,
"item" : "New Item",
"status" : "P",
"points" : 0,
"lastModified" : ISODate("2020-01-23T04:27:17.780Z")
}
给本页内容打分

在此页面上