MongoDB 更新文档
updateOne / updateMany 配合 $set 等更新操作符;update(旧 API)默认只改第一条;replaceOne 整体替换。
Key PointsMongoDB,updateOne,updateMany,$set,更新
讲解
updateOne / updateMany 配合 $set 等更新操作符;update(旧 API)默认只改第一条;replaceOne 整体替换。
示例
JavaScript
db.site.updateOne(
{ title: "Google" },
{ $set: { alexa: 1 } }
)
db.site.updateMany({ alexa: { $exists: false } }, { $set: { alexa: 0 } })Output
{
acknowledged: true,
modifiedCount: 1
}讲解
updateOne / updateMany 配合 $set 等更新操作符;update(旧 API)默认只改第一条;replaceOne 整体替换。
示例
JavaScript
db.site.updateOne(
{ title: "Google" },
{ $set: { alexa: 1 } }
)
db.site.updateMany({ alexa: { $exists: false } }, { $set: { alexa: 0 } })Output
{
acknowledged: true,
modifiedCount: 1
}