gpt4 book ai didi

mongodb - 将新字段添加到嵌套数组中的所有文档

转载 作者:行者123 更新时间:2023-12-02 01:55:18 24 4
gpt4 key购买 nike

我有一个个人文档数据库。每个都有一个名为 photos 的字段,它是照片文档的数组。我想向每个照片文档添加一个新的“已审阅”标志并将其初始化为 false。

这是我尝试使用的查询:

db.person.update({ "_id" : { $exists : true } }, {$set : {photos.reviewed : false} }, false, true)

但是我收到以下错误:

SyntaxError: missing : after property id (shell):1

这可能吗?如果可以,我的更新中做错了什么?

以下是“person”文档的完整示例:

{
"_class" : "com.foo.Person",
"_id" : "2894",
"name" : "Pixel Spacebag",
"photos" : [
{
"_id" : null,
"thumbUrl" : "http://site.com/a_s.jpg",
"fullUrl" : "http://site.com/a.jpg"
},
{
"_id" : null,
"thumbUrl" : "http://site.com/b_s.jpg",
"fullUrl" : "http://site.com/b.jpg"
}]
}

任何人如果能告诉我为什么要更新“所有文档”而不使用查询{ "_id": { $exists : true } }

最佳答案

对于那些仍在寻找答案的人,可以使用带有全位置运算符 $[] 的 MongoDB 3.6,请参阅 docs :

db.getCollection('person').update(
{},
{ $set: { "photos.$[].reviewed" : false } },
{ multi: true}
)

关于mongodb - 将新字段添加到嵌套数组中的所有文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9677044/

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