【问题标题】:How to iterate through result object after querying in mongo DB shell在 mongoDB shell 中查询后如何遍历结果对象
【发布时间】:2014-02-03 08:53:28
【问题描述】:

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

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

这似乎没有效果。 DB 不变。我正在从mongo shell 尝试这个。

【问题讨论】:

    标签: mongodb mongo-shell


    【解决方案1】:
    result['result'].forEach(function(doc){
        db.students.update(
            {'_id': doc._id}, 
            {'$pull':{'scores': {'score': doc.minitem }}}
        );
    });
    

    在 Mongo Shell 中使用 forEach 函数。

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

    【讨论】:

    • result['result'].forEach(function(doc){db.students.update( { '_id': result['_id'] },{ '$pull': { 'scores '{'score': 结果['miniitem'] } } })});我试过这个。我收到一个错误的意外令牌 {
    • @PrasannaAarthi 随时:)
    猜你喜欢
    • 1970-01-01
    • 2021-10-22
    • 2015-12-16
    • 2016-07-08
    • 2023-02-15
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    相关资源
    最近更新 更多