gpt4 book ai didi

mongodb - 如何在 mongo DB shell 中查询后遍历结果对象

转载 作者:可可西里 更新时间:2023-11-01 09:36:39 25 4
gpt4 key购买 nike

我在变量结果中有一个类似 [{_id:64,minitem:30},{},{}...] 的 JSON 数组对象。这是结果聚合操作,它是最小分数的投影。我需要迭代这个结果并从数据库中删除 minvales。我无法遍历结果对象。我试过了

for(i=0; i<result.length; i++){ 
db.students.update({
'_id': result['_id']
}, {
'$pull': { 'scores': { 'score': result[i]['minitem'] } }
})
}

这似乎没有效果。数据库不变。我在 mongo shell 中尝试这个。

最佳答案

result['result'].forEach(function(doc){
db.students.update(
{'_id': doc._id},
{'$pull':{'scores': {'score': doc.minitem }}}
);
});

使用 Mongo Shell 中的 forEach 函数。

请注意,聚合后文档实际上位于当前返回文档中的 result 元素中。

关于mongodb - 如何在 mongo DB shell 中查询后遍历结果对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21523038/

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