3

I have a situation where I need to combine multiple update queries into single query:

db.collection.update([ when name = 'roshan' then  set class = 1,  when name = 'John' then set class=2 ])

In sql this can be done using update statement with case statement like:

update set class =(case when name = 'roshan' then 1 when name = 'John' then 2 end)

How could it be done in mongodb?

Note: update query in mongo provides only 1 update condition. I have different update condition and different update values as mentioned above.

bor
  • 2,211
  • 4
  • 22
  • 37
  • I'm not quite sure what you're asking for... In case you have to update multiple documents and want to make sure all or none of them are updated, maybe [transactions](https://www.mongodb.com/transactions) are the thing you're looking for?! – Capricorn Aug 03 '18 at 08:44
  • @capricon no just multiple condition based update is what i need. – bor Aug 03 '18 at 08:54
  • 1
    Well, than this could be considered a duplicate of https://stackoverflow.com/questions/36698569/mongodb-update-with-condition, also note the "possible duplicate" of that linked question – Capricorn Aug 03 '18 at 08:56
  • @Capricorn update the description more detailed – bor Aug 03 '18 at 10:39
  • Yes, I think I now understand what you're looking for, that's why I posted the link to the possible duplicate question. – Capricorn Aug 03 '18 at 15:29

0 Answers0