简体   繁体   中英

how create mongoDB collection using data of another collection in same database

this is my one document of a collection in the mongoDB database

id: 1
calItems: Array
 0:Object
  Ts: 2016-05-01T00:00:00.000+00:00
  value: 5
 1:Object
  Ts: 2016-05-02T00:00:00.000+00:00
  value: 8

I need to create another collection with sum of these values for all documents

id:1
value: ?

You can use $out for creating new collection.

db.items.aggregate([{
                      $project : { sumis:  {$sum : "$caltems.value"}}
                    }, 
                    { $out: "newCollection" }
                   ])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM