Is it possible to get a self-referenced value in an update query in MongoDB? In MySQL, you can run:
UPDATE table SET column1 = column2 + column3,...
From what I know, only MapReduce can be used to do this while server-side in MongoDB. How do I use MapReduce to do this?
Is it possible to get a self-referenced value in an update query in MongoDB?
No, this update is not possible with the normal querying/updating system.
From what I know, only MapReduce can be used to do this while server-side in MongoDB.
Map / Reduce is used to summarize existing data and output that data to a separate collection/table. Map / Reduce is not intended to update existing data.
To run this update with MongoDB you will need to run a simple for loop over the entire collection, updating each one. You can do this from any of the drivers including the shell.
db.table.find().forEach( function(x) {
var newValue = x.column2 + x.column3; // Add column2 & 3
db.table.update({_id: x._id}, { $set: { column1: newValue } }); // Set the value on column1
} )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With