选择语言
On this page本页内容
➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples.使用右上角的“选择语言”下拉菜单设置以下示例的语言。
This page uses the following 本页面使用以下mongosh
methods:mongosh
方法:
The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To create and/or populate the 要创建和/或填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page uses MongoDB Compass to update the documents.此页面使用MongoDB Compass更新文档。
The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。Populate the 使用以下文档填充inventory
collection with the following documents:inventory
集合:
This page uses the following MongoDB C# Driver methods:此页面使用以下MongoDB C#驱动程序方法:
The examples on this page use the inventory
collection. To create and/or populate the inventory
collection, run the following:
This page uses the following MongoDB Go Driver functions:
The examples on this page use the inventory
collection. To create and/or populate the inventory
collection, run the following:
This page uses the following Java Reactive Streams Driver methods:
com.mongodb.reactivestreams.client.MongoCollection.updateOne
com.mongodb.reactivestreams.client.MongoCollection.updateMany
com.mongodb.reactivestreams.client.MongoCollection.replaceOne
The examples on this page use the inventory
collection. To create and/or populate the inventory
collection, run the following:
This page uses the following Java Synchronous Driver methods:
The examples on this page use the inventory
collection. To create and/or populate the inventory
collection, run the following:
This page uses the following Motor driver methods:
The examples on this page use the inventory
collection. To create and/or populate the inventory
collection, run the following:
This page uses the following MongoDB Node.js Driver methods:此页面使用以下MongoDB Node.js驱动程序方法:
The examples on this page use the 本页上的示例使用inventory
collection. inventory
集合。To create and/or populate the 要创建和/或填充inventory
collection, run the following:inventory
集合,请运行以下操作:
This page uses the following MongoDB Perl Driver methods:
The examples on this page use the inventory
collection. To create and/or populate the inventory
collection, run the following:
This page uses the following MongoDB PHP Library methods:
The examples on this page use the inventory
collection. To create and/or populate the inventory
collection, run the following:
This page uses the following PyMongo Python driver methods:
The examples on this page use the inventory
collection. To create and/or populate the inventory
collection, run the following:
This page uses the following MongoDB Ruby Driver methods:
The examples on this page use the inventory
collection. To create and/or populate the inventory
collection, run the following:
This page uses the following MongoDB Scala Driver methods:
The examples on this page use the inventory
collection. To create and/or populate the inventory
collection, run the following:
db.inventory.insertMany( [ { item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" }, { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" }, { item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "cm" }, status: "A" }, { item: "mousepad", qty: 25, size: { h: 19, w: 22.85, uom: "cm" }, status: "P" }, { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" }, { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" }, { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" }, { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }, { item: "sketchbook", qty: 80, size: { h: 14, w: 21, uom: "cm" }, status: "A" }, { item: "sketch pad", qty: 95, size: { h: 22.85, w: 30.5, uom: "cm" }, status: "A" } ] );
You can run the operation in the web shell below:您可以在下面的web shell中运行该操作:
[ { "item": "canvas", "qty": 100, "size": { "h": 28, "w": 35.5, "uom": "cm" }, "status": "A" }, { "item": "journal", "qty": 25, "size": { "h": 14, "w": 21, "uom": "cm" }, "status": "A" }, { "item": "mat", "qty": 85, "size": { "h": 27.9, "w": 35.5, "uom": "cm" }, "status": "A" }, { "item": "mousepad", "qty": 25, "size": { "h": 19, "w": 22.85, "uom": "cm" }, "status": "P" }, { "item": "notebook", "qty": 50, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "P" }, { "item": "paper", "qty": 100, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "D" }, { "item": "planner", "qty": 75, "size": { "h": 22.85, "w": 30, "uom": "cm" }, "status": "D" }, { "item": "postcard", "qty": 45, "size": { "h": 10, "w": 15.25, "uom": "cm" }, "status": "A" }, { "item": "sketchbook", "qty": 80, "size": { "h": 14, "w": 21, "uom": "cm" }, "status": "A" }, { "item": "sketch pad", "qty": 95, "size": { "h": 22.85, "w": 30.5, "uom": "cm" }, "status": "A" } ]
For instructions on inserting documents using MongoDB Compass, see Insert Documents.有关使用MongoDB Compass插入文档的说明,请参阅插入文档。
var documents = new[] { new BsonDocument { { "item", "canvas" }, { "qty", 100 }, { "size", new BsonDocument { { "h", 28 }, { "w", 35.5 }, { "uom", "cm" } } }, { "status", "A" } }, new BsonDocument { { "item", "journal" }, { "qty", 25 }, { "size", new BsonDocument { { "h", 14 }, { "w", 21 }, { "uom", "cm" } } }, { "status", "A" } }, new BsonDocument { { "item", "mat" }, { "qty", 85 }, { "size", new BsonDocument { { "h", 27.9 }, { "w", 35.5 }, { "uom", "cm" } } }, { "status", "A" } }, new BsonDocument { { "item", "mousepad" }, { "qty", 25 }, { "size", new BsonDocument { { "h", 19 }, { "w", 22.85 }, { "uom", "cm" } } }, { "status", "P" } }, new BsonDocument { { "item", "notebook" }, { "qty", 50 }, { "size", new BsonDocument { { "h", 8.5 }, { "w", 11 }, { "uom", "in" } } }, { "status", "P" } }, new BsonDocument { { "item", "paper" }, { "qty", 100 }, { "size", new BsonDocument { { "h", 8.5 }, { "w", 11 }, { "uom", "in" } } }, { "status", "D" } }, new BsonDocument { { "item", "planner" }, { "qty", 75 }, { "size", new BsonDocument { { "h", 22.85 }, { "w", 30 }, { "uom", "cm" } } }, { "status", "D" } }, new BsonDocument { { "item", "postcard" }, { "qty", 45 }, { "size", new BsonDocument { { "h", 10 }, { "w", 15.25 }, { "uom", "cm" } } }, { "status", "A" } }, new BsonDocument { { "item", "sketchbook" }, { "qty", 80 }, { "size", new BsonDocument { { "h", 14 }, { "w", 21 }, { "uom", "cm" } } }, { "status", "A" } }, new BsonDocument { { "item", "sketch pad" }, { "qty", 95 }, { "size", new BsonDocument { { "h", 22.85 }, { "w", 30.5 }, { "uom", "cm" } } }, { "status", "A" } }, }; collection.InsertMany(documents);
docs := []interface{}{ bson.D{ {"item", "canvas"}, {"qty", 100}, {"size", bson.D{ {"h", 28}, {"w", 35.5}, {"uom", "cm"}, }}, {"status", "A"}, }, bson.D{ {"item", "journal"}, {"qty", 25}, {"size", bson.D{ {"h", 14}, {"w", 21}, {"uom", "cm"}, }}, {"status", "A"}, }, bson.D{ {"item", "mat"}, {"qty", 85}, {"size", bson.D{ {"h", 27.9}, {"w", 35.5}, {"uom", "cm"}, }}, {"status", "A"}, }, bson.D{ {"item", "mousepad"}, {"qty", 25}, {"size", bson.D{ {"h", 19}, {"w", 22.85}, {"uom", "in"}, }}, {"status", "P"}, }, bson.D{ {"item", "notebook"}, {"qty", 50}, {"size", bson.D{ {"h", 8.5}, {"w", 11}, {"uom", "in"}, }}, {"status", "P"}, }, bson.D{ {"item", "paper"}, {"qty", 100}, {"size", bson.D{ {"h", 8.5}, {"w", 11}, {"uom", "in"}, }}, {"status", "D"}, }, bson.D{ {"item", "planner"}, {"qty", 75}, {"size", bson.D{ {"h", 22.85}, {"w", 30}, {"uom", "cm"}, }}, {"status", "D"}, }, bson.D{ {"item", "postcard"}, {"qty", 45}, {"size", bson.D{ {"h", 10}, {"w", 15.25}, {"uom", "cm"}, }}, {"status", "A"}, }, bson.D{ {"item", "sketchbook"}, {"qty", 80}, {"size", bson.D{ {"h", 14}, {"w", 21}, {"uom", "cm"}, }}, {"status", "A"}, }, bson.D{ {"item", "sketch pad"}, {"qty", 95}, {"size", bson.D{ {"h", 22.85}, {"w", 30.5}, {"uom", "cm"}, }}, {"status", "A"}, }, } result, err := coll.InsertMany(context.TODO(), docs)
Publisher<Success> insertManyPublisher = collection.insertMany(asList( Document.parse("{ item: 'canvas', qty: 100, size: { h: 28, w: 35.5, uom: 'cm' }, status: 'A' }"), Document.parse("{ item: 'journal', qty: 25, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }"), Document.parse("{ item: 'mat', qty: 85, size: { h: 27.9, w: 35.5, uom: 'cm' }, status: 'A' }"), Document.parse("{ item: 'mousepad', qty: 25, size: { h: 19, w: 22.85, uom: 'cm' }, status: 'P' }"), Document.parse("{ item: 'notebook', qty: 50, size: { h: 8.5, w: 11, uom: 'in' }, status: 'P' }"), Document.parse("{ item: 'paper', qty: 100, size: { h: 8.5, w: 11, uom: 'in' }, status: 'D' }"), Document.parse("{ item: 'planner', qty: 75, size: { h: 22.85, w: 30, uom: 'cm' }, status: 'D' }"), Document.parse("{ item: 'postcard', qty: 45, size: { h: 10, w: 15.25, uom: 'cm' }, status: 'A' }"), Document.parse("{ item: 'sketchbook', qty: 80, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }"), Document.parse("{ item: 'sketch pad', qty: 95, size: { h: 22.85, w: 30.5, uom: 'cm' }, status: 'A' }") ));
collection.insertMany(asList( Document.parse("{ item: 'canvas', qty: 100, size: { h: 28, w: 35.5, uom: 'cm' }, status: 'A' }"), Document.parse("{ item: 'journal', qty: 25, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }"), Document.parse("{ item: 'mat', qty: 85, size: { h: 27.9, w: 35.5, uom: 'cm' }, status: 'A' }"), Document.parse("{ item: 'mousepad', qty: 25, size: { h: 19, w: 22.85, uom: 'cm' }, status: 'P' }"), Document.parse("{ item: 'notebook', qty: 50, size: { h: 8.5, w: 11, uom: 'in' }, status: 'P' }"), Document.parse("{ item: 'paper', qty: 100, size: { h: 8.5, w: 11, uom: 'in' }, status: 'D' }"), Document.parse("{ item: 'planner', qty: 75, size: { h: 22.85, w: 30, uom: 'cm' }, status: 'D' }"), Document.parse("{ item: 'postcard', qty: 45, size: { h: 10, w: 15.25, uom: 'cm' }, status: 'A' }"), Document.parse("{ item: 'sketchbook', qty: 80, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }"), Document.parse("{ item: 'sketch pad', qty: 95, size: { h: 22.85, w: 30.5, uom: 'cm' }, status: 'A' }") ));
await db.inventory.insert_many( [ { "item": "canvas", "qty": 100, "size": {"h": 28, "w": 35.5, "uom": "cm"}, "status": "A", }, { "item": "journal", "qty": 25, "size": {"h": 14, "w": 21, "uom": "cm"}, "status": "A", }, { "item": "mat", "qty": 85, "size": {"h": 27.9, "w": 35.5, "uom": "cm"}, "status": "A", }, { "item": "mousepad", "qty": 25, "size": {"h": 19, "w": 22.85, "uom": "cm"}, "status": "P", }, { "item": "notebook", "qty": 50, "size": {"h": 8.5, "w": 11, "uom": "in"}, "status": "P", }, { "item": "paper", "qty": 100, "size": {"h": 8.5, "w": 11, "uom": "in"}, "status": "D", }, { "item": "planner", "qty": 75, "size": {"h": 22.85, "w": 30, "uom": "cm"}, "status": "D", }, { "item": "postcard", "qty": 45, "size": {"h": 10, "w": 15.25, "uom": "cm"}, "status": "A", }, { "item": "sketchbook", "qty": 80, "size": {"h": 14, "w": 21, "uom": "cm"}, "status": "A", }, { "item": "sketch pad", "qty": 95, "size": {"h": 22.85, "w": 30.5, "uom": "cm"}, "status": "A", }, ] )
await db.collection('inventory').insertMany([ { item: 'canvas', qty: 100, size: { h: 28, w: 35.5, uom: 'cm' }, status: 'A' }, { item: 'journal', qty: 25, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }, { item: 'mat', qty: 85, size: { h: 27.9, w: 35.5, uom: 'cm' }, status: 'A' }, { item: 'mousepad', qty: 25, size: { h: 19, w: 22.85, uom: 'cm' }, status: 'P' }, { item: 'notebook', qty: 50, size: { h: 8.5, w: 11, uom: 'in' }, status: 'P' }, { item: 'paper', qty: 100, size: { h: 8.5, w: 11, uom: 'in' }, status: 'D' }, { item: 'planner', qty: 75, size: { h: 22.85, w: 30, uom: 'cm' }, status: 'D' }, { item: 'postcard', qty: 45, size: { h: 10, w: 15.25, uom: 'cm' }, status: 'A' }, { item: 'sketchbook', qty: 80, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }, { item: 'sketch pad', qty: 95, size: { h: 22.85, w: 30.5, uom: 'cm' }, status: 'A' } ]);
$db->coll("inventory")->insert_many( [ { item => "canvas", qty => 100, size => { h => 28, w => 35.5, uom => "cm" }, status => "A" }, { item => "journal", qty => 25, size => { h => 14, w => 21, uom => "cm" }, status => "A" }, { item => "mat", qty => 85, size => { h => 27.9, w => 35.5, uom => "cm" }, status => "A" }, { item => "mousepad", qty => 25, size => { h => 19, w => 22.85, uom => "cm" }, status => "P" }, { item => "notebook", qty => 50, size => { h => 8.5, w => 11, uom => "in" }, status => "P" }, { item => "paper", qty => 100, size => { h => 8.5, w => 11, uom => "in" }, status => "D" }, { item => "planner", qty => 75, size => { h => 22.85, w => 30, uom => "cm" }, status => "D" }, { item => "postcard", qty => 45, size => { h => 10, w => 15.25, uom => "cm" }, status => "A" }, { item => "sketchbook", qty => 80, size => { h => 14, w => 21, uom => "cm" }, status => "A" }, { item => "sketch pad", qty => 95, size => { h => 22.85, w => 30.5, uom => "cm" }, status => "A" } ] );
$insertManyResult = $db->inventory->insertMany([ [ 'item' => 'canvas', 'qty' => 100, 'size' => ['h' => 28, 'w' => 35.5, 'uom' => 'cm'], 'status' => 'A', ], [ 'item' => 'journal', 'qty' => 25, 'size' => ['h' => 14, 'w' => 21, 'uom' => 'cm'], 'status' => 'A', ], [ 'item' => 'mat', 'qty' => 85, 'size' => ['h' => 27.9, 'w' => 35.5, 'uom' => 'cm'], 'status' => 'A', ], [ 'item' => 'mousepad', 'qty' => 25, 'size' => ['h' => 19, 'w' => 22.85, 'uom' => 'cm'], 'status' => 'P', ], [ 'item' => 'notebook', 'qty' => 50, 'size' => ['h' => 8.5, 'w' => 11, 'uom' => 'in'], 'status' => 'P', ], [ 'item' => 'paper', 'qty' => 100, 'size' => ['h' => 8.5, 'w' => 11, 'uom' => 'in'], 'status' => 'D', ], [ 'item' => 'planner', 'qty' => 75, 'size' => ['h' => 22.85, 'w' => 30, 'uom' => 'cm'], 'status' => 'D', ], [ 'item' => 'postcard', 'qty' => 45, 'size' => ['h' => 10, 'w' => 15.25, 'uom' => 'cm'], 'status' => 'A', ], [ 'item' => 'sketchbook', 'qty' => 80, 'size' => ['h' => 14, 'w' => 21, 'uom' => 'cm'], 'status' => 'A', ], [ 'item' => 'sketch pad', 'qty' => 95, 'size' => ['h' => 22.85, 'w' => 30.5, 'uom' => 'cm'], 'status' => 'A', ], ]);
db.inventory.insert_many( [ { "item": "canvas", "qty": 100, "size": {"h": 28, "w": 35.5, "uom": "cm"}, "status": "A", }, { "item": "journal", "qty": 25, "size": {"h": 14, "w": 21, "uom": "cm"}, "status": "A", }, { "item": "mat", "qty": 85, "size": {"h": 27.9, "w": 35.5, "uom": "cm"}, "status": "A", }, { "item": "mousepad", "qty": 25, "size": {"h": 19, "w": 22.85, "uom": "cm"}, "status": "P", }, { "item": "notebook", "qty": 50, "size": {"h": 8.5, "w": 11, "uom": "in"}, "status": "P", }, { "item": "paper", "qty": 100, "size": {"h": 8.5, "w": 11, "uom": "in"}, "status": "D", }, { "item": "planner", "qty": 75, "size": {"h": 22.85, "w": 30, "uom": "cm"}, "status": "D", }, { "item": "postcard", "qty": 45, "size": {"h": 10, "w": 15.25, "uom": "cm"}, "status": "A", }, { "item": "sketchbook", "qty": 80, "size": {"h": 14, "w": 21, "uom": "cm"}, "status": "A", }, { "item": "sketch pad", "qty": 95, "size": {"h": 22.85, "w": 30.5, "uom": "cm"}, "status": "A", }, ] )
client[:inventory].insert_many([ { item: 'canvas', qty: 100, size: { h: 28, w: 35.5, uom: 'cm' }, status: 'A' }, { item: 'journal', qty: 25, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }, { item: 'mat', qty: 85, size: { h: 27.9, w: 35.5, uom: 'cm' }, status: 'A' }, { item: 'mousepad', qty: 25, size: { h: 19, w: 22.85, uom: 'cm' }, status: 'P' }, { item: 'notebook', qty: 50, size: { h: 8.5, w: 11, uom: 'in' }, status: 'P' }, { item: 'paper', qty: 100, size: { h: 8.5, w: 11, uom: 'in' }, status: 'D' }, { item: 'planner', qty: 75, size: { h: 22.85, w: 30, uom: 'cm' }, status: 'D' }, { item: 'postcard', qty: 45, size: { h: 10, w: 15.25, uom: 'cm' }, status: 'A' }, { item: 'sketchbook', qty: 80, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }, { item: 'sketch pad', qty: 95, size: { h: 22.85, w: 30.5, uom: 'cm' }, status: 'A' } ])
collection.insertMany(Seq( Document("""{ item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" }"""), Document("""{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" }"""), Document("""{ item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "cm" }, status: "A" }"""), Document("""{ item: "mousepad", qty: 25, size: { h: 19, w: 22.85, uom: "cm" }, status: "P" }"""), Document("""{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" }"""), Document("""{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" }"""), Document("""{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" }"""), Document("""{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }"""), Document("""{ item: "sketchbook", qty: 80, size: { h: 14, w: 21, uom: "cm" }, status: "A" }"""), Document("""{ item: "sketch pad", qty: 95, size: { h: 22.85, w: 30.5, uom: "cm" }, status: "A" }""") )).execute()
To update a document, MongoDB provides update operators, such as 为了更新文档,MongoDB提供了更新运算符(如$set
, to modify field values.$set
)来修改字段值。
To use the update operators, pass to the update methods an update document of the form:要使用更新运算符,请向更新方法传递表单的更新文档:
{ <update operator>: { <field1>: <value1>, ... }, <update operator>: { <field2>: <value2>, ... }, ... }
Some update operators, such as 如果字段不存在,某些更新运算符(如$set
, will create the field if the field does not exist. $set
)将创建该字段。See the individual update operator reference for details.有关详细信息,请参阅单个更新运算符参考。
To update a document in Compass, hover over the target document and click the pencil icon:要更新Compass中的文档,请将鼠标悬停在目标文档上,然后单击铅笔图标:
After clicking the pencil icon, the document enters edit mode:单击铅笔图标后,文档进入编辑模式:
You can now change the this document by clicking the item you wish to change and modifying the value.现在,您可以通过单击要更改的项目并修改值来更改此文档。
For detailed instructions on updating documents in Compass, refer to the Compass documentation or follow the example below.有关更新Compass中文档的详细说明,请参阅Compass文档或遵循以下示例。
Once you are satisfied with your changes, click Update to save the updated document.对更改感到满意后,单击“更新”保存更新的文档。
Click Cancel to revert any modifications made to the document and exit edit mode.单击“取消”以恢复对文档所做的任何修改并退出编辑模式。
To update a document, MongoDB provides update operators such as $set
to modify field values.
To use the update operators, pass to the update methods an update document of the form:
{ <update operator> => { <field1> => <value1>, ... }, <update operator> => { <field2> => <value2>, ... }, ... }
Some update operators, such as $set
, will create the field if the field does not exist. See the individual update operator reference for details.
To update a document, MongoDB provides update operators such as $set
to modify field values.
Some update operators, such as $set
, will create the field if the field does not exist. See the individual update operator reference for details.
To update a document, MongoDB provides update operators such as $set
to modify field values.
The driver provides the com.mongodb.client.model.Updates class to facilitate the creation of update documents. For Example:例如:
combine(set( <field1>, <value1>), set(<field2>, <value2> ) )
For a list of the update helpers, see com.mongodb.client.model.Updates.
Some update operators, such as $set
, will create the field if the field does not exist. See the individual update operator reference for details.
To update a document, MongoDB provides update operators such as $set
to modify field values.
The driver provides the com.mongodb.client.model.Updates class to facilitate the creation of update documents. For Example:例如:
combine(set( <field1>, <value1>), set(<field2>, <value2> ) )
For a list of the update helpers, see com.mongodb.client.model.Updates.
Some update operators, such as $set
, will create the field if the field does not exist. See the individual update operator reference for details.
To update a document, MongoDB provides update operators such as 为了更新文档,MongoDB提供了更新运算符,如$set
to modify field values.$set
来修改字段值。
To use the update operators, pass to the update methods an update document of the form:要使用更新运算符,请向更新方法传递表单的更新文档:
{ <update operator>: { <field1>: <value1>, ... }, <update operator>: { <field2>: <value2>, ... }, ... }
Some update operators, such as $set
, will create the field if the field does not exist. See the individual update operator reference for details.
To update a document, MongoDB provides update operators such as 为了更新文档,MongoDB提供了更新运算符,如$set
to modify field values.$set
来修改字段值。
To use the update operators, pass to the update methods an update document of the form:要使用更新运算符,请向更新方法传递表单的更新文档:
{ <update operator>: { <field1>: <value1>, ... }, <update operator>: { <field2>: <value2>, ... }, ... }
Some update operators, such as 如果字段不存在,某些更新运算符(如$set
, will create the field if the field does not exist. $set
)将创建该字段。See the individual update operator reference for details.有关详细信息,请参阅单个更新运算符参考。
To update a document, MongoDB provides update operators such as $set
to modify field values.
To use the update operators, pass to the update methods an update document of the form:
{ <update operator> => { <field1> => <value1>, ... }, <update operator> => { <field2> => <value2>, ... }, ... }
Some update operators, such as $set
, will create the field if the field does not exist. See the individual update operator reference for details.
To update a document, MongoDB provides update operators such as $set
to modify field values.
To use the update operators, pass to the update methods an update document of the form:
[ <update operator> => [ <field1> => <value1>, ... ], <update operator> => [ <field2> => <value2>, ... ], ... ]
Some update operators, such as $set
, will create the field if the field does not exist. See the individual update operator reference for details.
To update a document, MongoDB provides update operators such as $set
to modify field values.
To use the update operators, pass to the update methods an update document of the form:
{ <update operator>: { <field1>: <value1>, ... }, <update operator>: { <field2>: <value2>, ... }, ... }
Some update operators, such as $set
, will create the field if the field does not exist. See the individual update operator reference for details.
To update a document, MongoDB provides update operators such as $set
to modify field values.
To use the update operators, pass to the update methods an update document of the form:
{ <update operator> => { <field1> => <value1>, ... }, <update operator> => { <field2> => <value2>, ... }, ... }
Some update operators, such as $set
, will create the field if the field does not exist. See the individual update operator reference for details.
To update a document, MongoDB provides update operators such as $set
to modify field values.
To use the update operators, pass to the update methods an update document of the form:
( set (<field1>, <value1>), set (<field2>, <value2>), ... )
Some update operators, such as $set
, will create the field if the field does not exist. See the individual update operator reference for details.
Starting in MongoDB 4.2, MongoDB can accept an aggregation pipeline to specify the modifications to make instead of an update document. See the method reference page for details.从MongoDB 4.2开始,MongoDB可以接受聚合管道来指定要进行的修改,而不是更新文档。有关详细信息,请参阅方法参考页。
The following example uses the 下面的示例使用db.collection.updateOne()
method on the inventory
collection to update the first document where item
equals "paper"
:inventory
集合上的db.collection.updateOne()
方法来更新item
等于"paper"
的第一个文档:
The following example demonstrates using MongoDB Compass to modify a single document where 以下示例演示如何使用MongoDB Compass修改item: paper
in the inventory
collection:inventory
集合中item:paper
所在的单个文档:
This example uses the Compass Table View to modify the document. 此示例使用Compass表视图修改文档。The editing process using the Compass List View follows a very similar approach.使用Compass列表视图的编辑过程遵循非常相似的方法。
For more information on the differences between Table View and List View in Compass, refer to the Compass documentation.有关Compass中表视图和列表视图之间差异的更多信息,请参阅Compass文档。
The following example uses the IMongoCollection.UpdateOne() method on the 下面的示例使用inventory
collection to update the first document where item
equals "paper"
:inventory
集合上的IMongoCollection.UpdateOne()方法来更新item
等于"paper"
的第一个文档:
The following example uses the Collection.UpdateOne method on the inventory
collection to update the first document where item
equals "paper"
:
The following example uses the com.mongodb.reactivestreams.client.MongoCollection.updateOne on the inventory
collection to update the first document where item
equals "paper"
:
The following example uses the com.mongodb.client.MongoCollection.updateOne method on the inventory
collection to update the first document where item
equals "paper"
:
The following example uses the update_one
method on the inventory
collection to update the first document where item
equals "paper"
:
The following example uses the Collection.updateOne() method on the 下面的示例使用inventory
collection to update the first document where item
equals "paper"
:inventory
集合上的Collection.updateOne()方法来更新项目等于“paper”的第一个文档:
The following example uses the update_one() method on the inventory
collection to update the first document where item
equals "paper"
:
The following example uses the updateOne()
method on the inventory
collection to update the first document where item
equals "paper"
:
The following example uses the update_one
method on the inventory
collection to update the first document where item
equals "paper"
:
The following example uses the update_one() method on the inventory
collection to update the first document where item
equals "paper"
:
The following example uses the updateOne() method on the inventory
collection to update the first document where item
equals "paper"
:
db.inventory.updateOne( { item: "paper" }, { $set: { "size.uom": "cm", status: "P" }, $currentDate: { lastModified: true } } )
The update operation:更新操作:
uses the 使用$set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,$set
运算符将size.uom
字段的值更新为"cm"
,将status
字段的值更改为"P"
,
uses the 使用$currentDate
operator to update the value of the lastModified
field to the current date. $currentDate
运算符将lastModified
字段的值更新为当前日期。If 如果lastModified
field does not exist, $currentDate
will create the field. lastModified
字段不存在,$currentDate
将创建该字段。See 有关详细信息,请参阅$currentDate
for details.$currentDate
。
Modify the target document as follows:按如下方式修改目标文档:
Change the 将status
field from D
to P
.status
字段从D
更改为P
。
Change the 将size.uom
field from in
to cm
.size.uom
字段从in
更改为cm
。
Add a new field called 添加一个名为lastModified
whose value will be today's date.lastModified
的新字段,其值将为今天的日期。
Click the Table button in the top navigation to access the Table View:单击顶部导航中的表格按钮以访问表格视图:
Use the Compass query bar to locate the target document.使用Compass查询栏查找目标文档。
Copy the following filter document into the query bar and click Find:将以下筛选文档复制到查询栏中,然后单击“查找”:
{ item: "paper" }
Hover over the 将鼠标悬停在status
field and click the pencil icon which appears on the right side of the document to enter edit mode:status
字段上,然后单击文档右侧显示的铅笔图标以进入编辑模式:
Change the value of the field to 将字段值更改为"P"
."P"
。
Click the Update button below the field to save your changes.单击字段下方的“更新”按钮保存更改。
Hover over the 将鼠标悬停在size
field and click the outward-pointing arrows which appear on the right side of the field. size
字段上,然后单击该字段右侧显示的向外箭头。This opens a new tab which displays the fields within the 这将打开一个新选项卡,其中显示size
object:size
对象中的字段:
Using the same process outlined in steps 3-5 for editing the 使用步骤3-5中概述的相同过程编辑status
field, change the value of the size.uom
field to "cm"
.status
字段,将size.uom
字段的值更改为"cm"
。
Click the left-most tab above the table labelled 单击标有inventory
to return to the original table view, which displays the top-level document:inventory
的表格上方最左侧的选项卡,返回原始表格视图,该视图显示顶级文档:
Hover over the 将鼠标悬停在status
field and click the pencil icon which appears on the right side of the document to re-enter edit mode.status
字段上,然后单击文档右侧显示的铅笔图标以重新进入编辑模式。
Click inside of the 单击status
field and click the plus button icon which appears in the edit menu.status
字段内部,然后单击编辑菜单中显示的“加号按钮”图标。
Click the Add Field After status button which appears below the plus button:单击加号按钮下方显示的“添加字段后”状态按钮:
Add a new field called 添加一个名为lastModified
with a value of today's date. lastModified
的新字段,其值为今天的日期。Set the field type to 将字段类型设置为Date
:Date
:
Click the Update button below the field to save your changes.单击字段下方的“更新”按钮保存更改。
Because MongoDB Compass does not support 由于MongoDB Compass不支持$currentDate
or any other Field Update Operators, you must manually enter the date value in Compass.$currentDate
或任何其他字段更新运算符,因此必须在Compass中手动输入日期值。
var filter = Builders<BsonDocument>.Filter.Eq("item", "paper"); var update = Builders<BsonDocument>.Update.Set("size.uom", "cm").Set("status", "P").CurrentDate("lastModified"); var result = collection.UpdateOne(filter, update);
The update operation:更新操作
uses the $set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,
uses the $currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.
result, err := coll.UpdateOne( context.TODO(), bson.D{ {"item", "paper"}, }, bson.D{ {"$set", bson.D{ {"size.uom", "cm"}, {"status", "P"}, }}, {"$currentDate", bson.D{ {"lastModified", true}, }}, }, )
The update operation:更新操作:
uses the $set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,
uses the $currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.
Publisher<UpdateResult> updateOnePublisher = collection.updateOne(eq("item", "paper"), combine(set("size.uom", "cm"), set("status", "P"), currentDate("lastModified")));
The update operation:更新操作
uses the $set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,
uses the $currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.
collection.updateOne(eq("item", "paper"), combine(set("size.uom", "cm"), set("status", "P"), currentDate("lastModified")));
The update operation:更新操作
uses the $set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,
uses the $currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.
await db.inventory.update_one( {"item": "paper"}, {"$set": {"size.uom": "cm", "status": "P"}, "$currentDate": {"lastModified": True}}, )
The update operation:更新操作:
uses the $set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,
uses the $currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.
await db.collection('inventory').updateOne( { item: 'paper' }, { $set: { 'size.uom': 'cm', status: 'P' }, $currentDate: { lastModified: true } } );
The update operation:更新操作:
uses the 使用$set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,$set
运算符将size.uom
字段的值更新为"cm"
,将status
字段的值更改为"P"
,
uses the 使用$currentDate
operator to update the value of the lastModified
field to the current date. $currentDate
运算符将lastModified
字段的值更新为当前日期。If 如果lastModified字段不存在,lastModified
field does not exist, $currentDate
will create the field. $currentDate
将创建该字段。See 有关详细信息,请参阅$currentDate
for details.$currentDate
。
# For boolean values, use boolean.pm for 'true' and 'false' $db->coll("inventory")->update_one( { item => "paper" }, { '$set' => { "size.uom" => "cm", status => "P" }, '$currentDate' => { lastModified => true } } );
The update operation:更新操作
uses the $set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,
uses the $currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.
$updateResult = $db->inventory->updateOne( ['item' => 'paper'], [ '$set' => ['size.uom' => 'cm', 'status' => 'P'], '$currentDate' => ['lastModified' => true], ] );
The update operation:更新操作
uses the $set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,
uses the $currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.
db.inventory.update_one( {"item": "paper"}, {"$set": {"size.uom": "cm", "status": "P"}, "$currentDate": {"lastModified": True}}, )
client[:inventory].update_one({ item: 'paper'}, { '$set' => { 'size.uom' => 'cm', 'status' => 'P' }, '$currentDate' => { 'lastModified' => true } })
The update operation:更新操作
uses the $set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,
uses the $currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.
collection.updateOne(equal("item", "paper"), combine(set("size.uom", "cm"), set("status", "P"), currentDate("lastModified")) ).execute()
The update operation:更新操作
uses the $set
operator to update the value of the size.uom
field to "cm"
and the value of the status
field to "P"
,
uses the $currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.
db.inventory.updateMany( { "qty": { $lt: 50 } }, { $set: { "size.uom": "in", status: "P" }, $currentDate: { lastModified: true } } )
The update operation:更新操作:
uses the 使用$set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,$set
运算符将size.uom
字段的值更新为"in"
,将status
字段的值更改为"P"
,
uses the 使用$currentDate
operator to update the value of the lastModified
field to the current date. $currentDate
运算符将lastModified
字段的值更新为当前日期。If 如果lastModified
field does not exist, $currentDate
will create the field. lastModified
字段不存在,$currentDate
将创建该字段。See 有关详细信息,请参阅$currentDate
for details.$currentDate
。
var filter = Builders<BsonDocument>.Filter.Lt("qty", 50); var update = Builders<BsonDocument>.Update.Set("size.uom", "in").Set("status", "P").CurrentDate("lastModified"); var result = collection.UpdateMany(filter, update);
The update operation:更新操作:
uses the $set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,
uses the $currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.
result, err := coll.UpdateMany( context.TODO(), bson.D{ {"qty", bson.D{ {"$lt", 50}, }}, }, bson.D{ {"$set", bson.D{ {"size.uom", "cm"}, {"status", "P"}, }}, {"$currentDate", bson.D{ {"lastModified", true}, }}, }, )
The update operation:更新操作:
uses the $set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,
uses the $currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.
Publisher<UpdateResult> updateManyPublisher = collection.updateMany(lt("qty", 50), combine(set("size.uom", "in"), set("status", "P"), currentDate("lastModified")));
The update operation:更新操作:
uses the $set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,
uses the $currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.
collection.updateMany(lt("qty", 50), combine(set("size.uom", "in"), set("status", "P"), currentDate("lastModified")));
The update operation:更新操作:
uses the $set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,
uses the $currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.
await db.inventory.update_many( {"qty": {"$lt": 50}}, {"$set": {"size.uom": "in", "status": "P"}, "$currentDate": {"lastModified": True}}, )
The update operation:更新操作:
uses the $set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,
uses the $currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.
await db.collection('inventory').updateMany( { qty: { $lt: 50 } }, { $set: { 'size.uom': 'in', status: 'P' }, $currentDate: { lastModified: true } } );
The update operation:更新操作:
uses the 使用$set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,$set
运算符将size.uom
字段的值更新为"in"
,将status
字段的值更改为"P"
,
uses the 使用$currentDate
operator to update the value of the lastModified
field to the current date. $currentDate
运算符将lastModified
字段的值更新为当前日期。If 如果lastModified
field does not exist, $currentDate
will create the field. lastModified
字段不存在,$currentDate
将创建该字段。See 有关详细信息,请参阅$currentDate
for details.$currentDate
。
# For boolean values, use boolean.pm for 'true' and 'false' $db->coll("inventory")->update_many( { qty => { '$lt' => 50 } }, { '$set' => { "size.uom" => "in", status => "P" }, '$currentDate' => { lastModified => true } } );
The update operation:更新操作:
uses the $set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,
uses the $currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.
$updateResult = $db->inventory->updateMany( ['qty' => ['$lt' => 50]], [ '$set' => ['size.uom' => 'cm', 'status' => 'P'], '$currentDate' => ['lastModified' => true], ] );
The update operation:更新操作:
uses the $set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,
uses the $currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.
db.inventory.update_many( {"qty": {"$lt": 50}}, {"$set": {"size.uom": "in", "status": "P"}, "$currentDate": {"lastModified": True}}, )
client[:inventory].update_many({ qty: { '$lt' => 50 } }, { '$set' => { 'size.uom' => 'in', 'status' => 'P' }, '$currentDate' => { 'lastModified' => true } })
The update operation:更新操作:
uses the $set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,
uses the $currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.
collection.updateMany(lt("qty", 50), combine(set("size.uom", "in"), set("status", "P"), currentDate("lastModified")) ).execute()
The update operation:更新操作:
uses the $set
operator to update the value of the size.uom
field to "in"
and the value of the status
field to "P"
,
uses the $currentDate
operator to update the value of the lastModified
field to the current date. If lastModified
field does not exist, $currentDate
will create the field. See $currentDate
for details.
db.inventory.replaceOne( { item: "paper" }, { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] } )
var filter = Builders<BsonDocument>.Filter.Eq("item", "paper"); var replacement = new BsonDocument { { "item", "paper" }, { "instock", new BsonArray { new BsonDocument { { "warehouse", "A" }, { "qty", 60 } }, new BsonDocument { { "warehouse", "B" }, { "qty", 40 } } } } }; var result = collection.ReplaceOne(filter, replacement);
result, err := coll.ReplaceOne( context.TODO(), bson.D{ {"item", "paper"}, }, bson.D{ {"item", "paper"}, {"instock", bson.A{ bson.D{ {"warehouse", "A"}, {"qty", 60}, }, bson.D{ {"warehouse", "B"}, {"qty", 40}, }, }}, }, )
Publisher<UpdateResult> replaceOnePublisher = collection.replaceOne(eq("item", "paper"), Document.parse("{ item: 'paper', instock: [ { warehouse: 'A', qty: 60 }, { warehouse: 'B', qty: 40 } ] }"));
collection.replaceOne(eq("item", "paper"), Document.parse("{ item: 'paper', instock: [ { warehouse: 'A', qty: 60 }, { warehouse: 'B', qty: 40 } ] }"));
await db.inventory.replace_one( {"item": "paper"}, { "item": "paper", "instock": [{"warehouse": "A", "qty": 60}, {"warehouse": "B", "qty": 40}], }, )
await db.collection('inventory').replaceOne( { item: 'paper' }, { item: 'paper', instock: [ { warehouse: 'A', qty: 60 }, { warehouse: 'B', qty: 40 } ] } );
$db->coll("inventory")->replace_one( { item => "paper" }, { item => "paper", instock => [ { warehouse => "A", qty => 60 }, { warehouse => "B", qty => 40 } ] } );
$updateResult = $db->inventory->replaceOne( ['item' => 'paper'], [ 'item' => 'paper', 'instock' => [ ['warehouse' => 'A', 'qty' => 60], ['warehouse' => 'B', 'qty' => 40], ], ] );
db.inventory.replace_one( {"item": "paper"}, { "item": "paper", "instock": [{"warehouse": "A", "qty": 60}, {"warehouse": "B", "qty": 40}], }, )
client[:inventory].replace_one({ item: 'paper' }, { item: 'paper', instock: [ { warehouse: 'A', qty: 60 }, { warehouse: 'B', qty: 40 } ] })
collection.replaceOne(equal("item", "paper"), Document("""{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] }""") ).execute()
All write operations in MongoDB are atomic on the level of a single document. MongoDB中的所有写入操作都是单个文档级别的原子操作。For more information on MongoDB and atomicity, see Atomicity and Transactions.有关MongoDB和原子性的更多信息,请参阅原子性和事务。
_id
Once set, you cannot update the value of the 设置后,您不能更新_id
field nor can you replace an existing document with a replacement document that has a different _id
field value._id
字段的值,也不能用具有不同_id
字段值的替换文档替换现有文档。
For write operations, MongoDB preserves the order of the document fields except for the following cases:对于写操作,MongoDB保留文档字段的顺序,但以下情况除外:
With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. 对于写操作,您可以指定从MongoDB请求的写操作确认级别。For details, see Write Concern.有关详细信息,请参阅写入关注点。