How to update based on existing data in mongo

Grade mongo noob, and I have a feeling that I am dealing with this problem from the wrong direction.

I have about 7 million documents. Each document has two fields that I want to change (not replace), basically these are large lines with \\n , and I want to replace them \n .

I spent about an hour trying to find a way to "return the link" to the object returned by the request, which does not completely exist. What is the best approach for something like this?

+6
mongodb
source share
2 answers

You will need to request all documents and update them one by one. If you do this in JavaScript, it will be something like this:

 mydb = db.getSisterDB("whateverDBYoureUsing"); var cursor = mydb.foo.find(); while (cursor.hasNext()) { var x = cursor.next(); /* replace \\n with \n in x strings ... */ db.foo.update({_id : x._id}, x); } 

You can copy this to a .js file (say replace.js), change the db and collection names and run it as a script from the shell:

 mongo replace.js 
+6
source share
0
source share

All Articles