-1

Given a collection of documents similar to the following document

{
"description": "janeetjack",
"name": "Pocog bistro janeetjack"
}

where name is a unique field

How do I update all existing documents and add additional fields so it looks like this

{
"userDetails":{
"description": "janeetjack",
"name": "Pocog bistro janeetjack"
},
"userLikes":{
"likes": "food",
"plays": "ball"
},

}
Bazinga_
  • 95
  • 1
  • 1
  • 5

1 Answers1

0

You need to run set and unset on all docs with updateMany

Playground

db.collection.update({},
{
  "$set": {
    "userDetails.description": "$description",
    "userDetails.name": "$name",
    "userLikes.like": "abs"
  },
  "$unset": {
    description: 1,
    name: 1
  }
})
Gibbs
  • 21,904
  • 13
  • 74
  • 138