sql >> Base de Datos >  >> NoSQL >> MongoDB

Agregación con actualización en mongoDB

Después de muchos problemas, experimentando con Mongo Shell, finalmente obtuve una solución a mi pregunta.

Psudocódigo:

# To get the list of customer whose score is greater than 2000
cust_to_clear=db.col.aggregate(
    {$match:{$or:[{status:'A'},{status:'B'}]}},
    {$group:{_id:'$cust_id',total:{$sum:'$score'}}},
    {$match:{total:{$gt:500}}})

# To loop through the result fetched from above code and update the clear
cust_to_clear.result.forEach
(
   function(x)
   { 
     db.col.update({cust_id:x._id},{$set:{clear:'Yes'}},{multi:true}); 
   }
)

Comente, si tiene alguna solución diferente para la misma pregunta.