Frage

I am having a JSON array object something like [{_id:64,minitem:30},{},{}...] inside a variable result.. This is the result of an aggregation operation which is a projection of minimum score. I need to iterate over this result and delete the minvales from DB. I am unable to loop over the result object .. I tried

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

This doesnt seem to have an effect. DB is unaltered. I am trying this from mongo shell.

War es hilfreich?

Lösung

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

Uses the forEach function within the Mongo Shell.

Note that after aggregation the documents are actually within a result element within the return document currently.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top