修改
语法
{
<update operator>: {
<field> : {
...
},
<field> : {
}
},
<update operator>: {
...
}
}
更新文档的顶层包含以下一个或多个更新运算符:
- $set:将某个字段的值替换为指定的值
- $inc:增加或减少字段值
- $rename: 重命名字段
- $unset:删除字段
- $mul:将字段值乘以指定数字
例子
const myDB = client.db("myDB");
const myColl = myDB.collection("items");
const filter = { _id: 465 };
// update the value of the 'quantity' field to 5
const updateDocument = {
$set: {
quantity: 5,
},
};
const result = await myColl.updateOne(filter, updateDocument);