gpt4 book ai didi

MongoDB 在数组中设置一个子项

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

在集合中

{ _id: 123,
category: 1,
desc: 'desc',
price: 99,
item:
[ { two: '24',
three: '48' },
{ two: '',
three:''},
{ two: '33',
three:'24'},
] } ,
{ _id: 121,
category: 1,
desc: 'desc',
price: 99,
item:
[ { two: '24',
three: '58' },
{ two: '',
three:''},
{ two: '35',
three:'54'},
] }

如何将所有记录的值“two”设置为 null,其中 item.two = '24'

最佳答案

使用位置运算符 $从匹配搜索查询的项目数组更新文档:

update({ "item.two" : "24" }, 
{ $set : { "item.$.two" : "" }}, false, true);

如果你想从数组文档中删除这个字段,那么使用$unset运算符(operator):

update({ "item.two" : "25" }, 
{ $unset : { "item.$.two" : "" }}, false, true);

关于MongoDB 在数组中设置一个子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19722548/

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