How to do multiple aggregation operations in MongoQuery and Update the field in a collection

久未见 提交于 2019-12-13 21:28:14

问题


In RDBMS, suppose we have 4 columns , say col1, col2, col3, col4

If we want col4 value to be col1 + col2 - col3, we can write an update statement as follows:

Update Table set col4=col1+col2-col3

Is doing multiple operations possibe using Mongo shell aggregation?

I tried to get the sum of two fields using aggregation with following query.

db.students.aggregate([    
    {      
        $project: { examTotal: { $add: [ "$final", "$midterm" ] } }
    },
    { $out: "students" }
])

But with this query its replacing the fields in the same collection. How could this be achieved?

col4=col1+col2-col3 and update the col4 value in same collection. Can this be possible within a single step?

来源:https://stackoverflow.com/questions/35240667/how-to-do-multiple-aggregation-operations-in-mongoquery-and-update-the-field-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!