How do I append Mongo DB aggregation results to an existing collection?

前端 未结 3 452
刺人心
刺人心 2021-01-11 09:45

I am trying to perform several insertions on an existent Mongo DB collection using the following code

db.dados_meteo.aggregate( [
                  { $match          


        
3条回答
  •  悲哀的现实
    2021-01-11 10:39

    It's not the prettiest thing ever, but as another alternative syntax (from a post-processing archive/append operation)...

    db.targetCollection.insertMany(db.runCommand(
    {
        aggregate: "sourceCollection",
        pipeline: 
        [
            { $skip: 0 },
            { $limit: 5 },
            { 
                $project:
                {
                    myObject: "$$ROOT",
                    processedDate: { $add: [new ISODate(), 0] }
                }
            }
        ]
    }).result)

    I'm not sure how this stacks up against the forEach variant, but i find it more intuitive to read.

提交回复
热议问题