作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我在变量结果中有一个类似 [{_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/
我是一名优秀的程序员,十分优秀!