gpt4 book ai didi

Mongodb - 更新部分文档

转载 作者:可可西里 更新时间:2023-11-01 10:34:50 25 4
gpt4 key购买 nike

在 Mongodb 中可以限制一次更新的文档数量吗?

我有一个按插入日期(即按 _id)排序的项目列表,我只想更新(示例)5 个最旧的项目。

我可以检索到我想要的信息

db.mycoll.find({}).limit(5)

我可以更新一个(因此循环上一个查询的结果)或所有(或使用组合)

db.mycoll.update({"_id" : ObjectId("4f672a5175fa1c01c100001d")}, {$set: {'b' : 'test'}}, false, true)
db.mycoll.update({}, {$set: {'b' : 'test'}}, false, true)

但是我无法通过对更新请求进行限制的标准。有什么办法吗?

最佳答案

不幸的是,你可以看看这个:how-to-limit-number-of-updating-documents-in-mongodb

Unfortunately the workaround you have is the only way to do it AFAIK. There is a boolean flag multi which will either update all the matches (when true) or update the 1st match (when false).

关于Mongodb - 更新部分文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9780642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com