db.collection.updateOne(filter,update,options)
顾名思义,通过这行声明,我们大概可以猜到这三个参数大概的作用:
- filter 用作在更新之前筛选符合条件的document
- update 具体更新document中哪些字段
- options 附加可选的额外操作
整体updateOne语句的语法如下:
db.collection.updateOne(
<filter>,
<update>,
{
upsert: <boolean>, //true的时候如果filter没有匹配到则将update的内容插入到集合collection
writeConcern: <document>, //写事物
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ],
hint: <document|string> // Available starting in MongoDB 4.2.1
}
)
案例一:更新某个文档
案例原始数据:
> db.user.find()
{
"_id" : ObjectId("60c20b6adebf968f925b0dde"), "name" : "peng" }
{
"_id" : ObjectId("60c20c98debf968f925b0ddf"), "name" : "jian" }
{
"_id" : ObjectId("60c20cb6debf968f925b0de0"), "name" : "song" }

本文详细介绍了MongoDB中updateOne方法的基本用法及其参数配置,包括如何通过filter筛选目标文档进行更新,如何设置upsert选项实现插入或更新操作,以及如何运用聚合管道实现更复杂的文档结构调整。

1636

被折叠的 条评论
为什么被折叠?



