9

Using Mongo shell, I am trying to add a field which has the same value as an existing field for all documents in a collection. Assuming we have two documents :

{
   'foo': 'str1'
}

and

{
   'foo': 'str2'
}

I would like to insert a new field 'foo_new' which has the respective value of 'foo' as its value, so that the documents become

{
   'foo': 'str1'
   'foo_new': 'str1'
}

and

{
   'foo': 'str2'
   'foo_new': 'str2'
}

The command I use to update the collection ('coll' say) in Mongo shell is

db.coll.update({}, {$set: {'foo_new': '$foo'}}, {multi: true})

The result of running this command are the two updated documents

{
   'foo': 'str1'
   'foo_new': '$foo'
}

and

{
   'foo': 'str2'
   'foo_new': '$foo'
}

i.e. '$foo' is being interpreted as a literal for some reason.

whoami - fakeFaceTrueSoul
  • 17,086
  • 6
  • 32
  • 46
John S.
  • 501
  • 2
  • 6
  • 17
  • Does this answer your question? [how to update a number field using another number field in MongoDB](https://stackoverflow.com/questions/60418883/how-to-update-a-number-field-using-another-number-field-in-mongodb) – whoami - fakeFaceTrueSoul Feb 28 '20 at 15:36
  • @whoami: In theory it does. It's exactly what I tried, but my Mongo shell would interpret the "$a" as the string '$a' not as 2, the value of the field whose key is 'a'. – John S. Feb 28 '20 at 17:01
  • 1
    S : There is a difference in what you've tried with what has to be done, `.update()` should be in aggregation pipeline :: `[ ]` not as an object !! What is your MongoDB version ? – whoami - fakeFaceTrueSoul Feb 28 '20 at 17:02
  • @whoami: I also tried putting the update in square brackets. Same issue unfortunately. MongoDB server version: 4.0.16 – John S. Feb 28 '20 at 17:05
  • Aggregation pipeline in `.updates()` do work only with MongoDB v >=4.2, As stated in answer, With versions below than that you need to first read & then write it back !! Or use an aggregation pipeline to completely create a different collection with required data & dropping existing one..If you're ok with that I can provide a query.. – whoami - fakeFaceTrueSoul Feb 28 '20 at 17:06
  • 1
    @whoami: Thanks for pointing out! I'll take the read & write back approach, but thanks for offering to provide a query. – John S. Feb 28 '20 at 17:12

1 Answers1

19

Try this snippet:

db.<collection>.update({}, [{$set: {'foo_new': '$foo'}}], {"multi": true})

Note the [] square brackets in 2nd argument. More info: https://stackoverflow.com/a/37280419/4050261

Adarsh Madrecha
  • 6,364
  • 11
  • 69
  • 117
Basu_C
  • 380
  • 1
  • 5
  • https://stackoverflow.com/questions/60418883/how-to-update-a-number-field-using-another-number-field-in-mongodb – whoami - fakeFaceTrueSoul Jul 15 '21 at 20:53
  • If we have create a new document, can we have the new field auto populated with this value. Is there a way to achieve this? I will basically have a calculation based on 2 other fields. Will the calculation reflect for the new field if we add a new document? – pranav Aug 09 '21 at 11:19
  • 1
    This isn't a valid solution for Mongo 4.0. – Kane Jul 26 '22 at 20:04
  • 1
    FWIW I ran into this same issue and didn't need to add the `{"multi": true}` portion, just adding the square brackets around my set criteria did the trick. – Major Major Jan 18 '23 at 01:44