-1
{
"a" :"2"
"b" : "3"
}

I want to create a new "c" that copied his data from "a" the new update will looks like the following:

{
 "a" : "2"
 "b" : "3"
 "c" : "2"
}

I tried with $addfileds but it does not work

db.letters.aggregate(
    [
        { "$addFields": { 
            "c": "$a"  
        }},
        { "$out": "collection" }
    ]
)
Mark
  • 5,994
  • 5
  • 42
  • 55
Tuz
  • 1,810
  • 5
  • 29
  • 58

1 Answers1

1

Give a try to the following one by using $project.

db.coll.aggregate([ { $project: { "a": 1, "b": 1, "c": "$a" }}]); 

Output:

{ "_id" : ObjectId("5c6becd138395c0c946c19a2"), "a" : "2", "b" : "3", "c" : "2" }

Doc: $project

Mark
  • 5,994
  • 5
  • 42
  • 55
  • 1
    Please just mark the question as a duplicate, as the question has already been answered (with a very highly upvoted answer giving different options for different MongoDB versions) - I linked to the duplicate in my comment. Note that the answer varies based on version of MongoDB being used. – David Makogon Feb 19 '19 at 11:57