Aggregation with update in mongoDB

后端 未结 5 1456
予麋鹿
予麋鹿 2020-12-05 23:47

I\'ve a collection with many similar structured document, two of the document looks like

Input:

{ 
    \"_id\": ObjectId(\"525c22348771ebd7b179add8\"         


        
5条回答
  •  盖世英雄少女心
    2020-12-06 00:32

    After a lot of trouble, experimenting mongo shell I've finally got a solution to my question.

    Psudocode:

    # 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}); 
       }
    )
    

    Please comment, if you have any different solution for the same question.

提交回复
热议问题