Reputation: 610
I would like to update a collection by transforming all documents from this form:
{
"_id" : "somestring i made",
"value" : {
"a" : 0.42361499999999996,
"b" : 3,
"c" : "foo",
"d" : "bar"
}
}
To this form (with new id's):
{
"_id" : ObjectId("77d987f6dsf6f76sa7676df"),
"a" : 0.42361499999999996,
"b" : 3,
"c" : "foo",
"d" : "bar"
}
So essentially take the fields out of the object "value" and reset the id to a real document id.
Upvotes: 1
Views: 107
Reputation: 12945
First get the document , convert to required format , remove the old doc and again insert the modified one . Something like
db.collection.find({}).forEach(function(doc){
var obj = { a : doc.value.a,
b : doc.value.b,
c : doc.value.c,
d : doc.value.d};
db.collection.remove(doc);
db.collection.insert(obj);
});
Upvotes: 1