阅读量:111
要修改MongoDB中的字段值,您可以使用update()方法或findOneAndUpdate()方法。
- 使用update()方法:
db.collection.update(
{ },
{ $set: { : , : , ... } }
)
示例:
db.collection.update(
{ _id: ObjectId("12345") },
{ $set: { name: "John", age: 30 } }
)
- 使用findOneAndUpdate()方法:
db.collection.findOneAndUpdate(
{ },
{ $set: { : , : , ... } }
)
示例:
db.collection.findOneAndUpdate(
{ _id: ObjectId("12345") },
{ $set: { name: "John", age: 30 } }
)
请注意,update()方法会更新所有符合条件的文档,而findOneAndUpdate()方法只会更新第一个符合条件的文档。