tags:

views:

40

answers:

2

Sort of a mongo noob, and I have a feeling I am tackling this problem from the wrong direction.

I have about a 7 million document collection. Each document has two fields that I want to modify (not replace), basically they are big strings that have \\n, and I want to replace those with \n.

I spent about an hour trying to find a way to "back reference" the object returned by the query, which totally doesn't exist. What is the best approach for something like this?

A: 

What you want is doing an upsert http://www.mongodb.org/display/DOCS/Updating#Updating-UpsertswithModifiers

Markus Gattol
What update modifier would he use for this?
Thilo
+1  A: 

You'll have to query for all the documents and update them one by one. If you do it in JavaScript, it would be something like:

mydb = db.getSisterDB("whateverDBYoureUsing");
var cursor = mydb.foo.find();
while (cursor.hasNext()) {
    var x = cursor.next();

    /* replace \\n with \n in x's strings ... */

    db.foo.update({_id : x._id}, x);
}

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

mongo replace.js
kristina
pretty much what I was looking for :)
Matt Briggs