有没有一种方法可以使用 "update" 函数在给定查询和排序选项的情况下更新 MongoDB 集合中的第一个文档?
Is there a way to update the first document in a MongoDB collection given a query and a sort option using the "update" function?
我想使用 MongoDB 中的 update
函数来更新集合中给定 query
和 sort
(或 orderBy
) 配置。
我知道 findAndModify
的解决方案:
db.col.findAndModify(query, sort, updates)
你能用 update
函数做同样的事情吗?
否 您不能将 sort
与 update
一起使用,但如果您未在更新中指定 multi
选项,则仅更新第一个文档满足 query 标准将被更新。
除非您使用 Capped collections.
,否则无法保证磁盘上文档的顺序
根据 UpdateOne 的文档指定一个空文档 { } 作为过滤器以更新集合中返回的第一个文档。
我想使用 MongoDB 中的 update
函数来更新集合中给定 query
和 sort
(或 orderBy
) 配置。
我知道 findAndModify
的解决方案:
db.col.findAndModify(query, sort, updates)
你能用 update
函数做同样的事情吗?
否 您不能将 sort
与 update
一起使用,但如果您未在更新中指定 multi
选项,则仅更新第一个文档满足 query 标准将被更新。
除非您使用 Capped collections.
根据 UpdateOne 的文档指定一个空文档 { } 作为过滤器以更新集合中返回的第一个文档。