I have a collection of a few million documents, with the format:
{
_id: ...
main: {
case: ...
csc: ...
}
}
and need it to transform into:
{
_id: ...
case: ...
csc: ...
}
I found an answer in stackoverflow, but there they use the mongo shell (synchronous). I would like to do the same in node using the mongodb driver.
I've tried cursor.each to loop over the documents, but the insert command needs a callback, so I'm stuck with that solution:
coll.find().each(function(err, item) {
newDoc.insert(item.main, function(err) {
???
})
});
Also I've tried and update:
update({}, {$set: {case: value.case, csc: value.csc}, function(err) {
...
});
which doesn't work (as you can not use value.case and value.csc in the $set argument?)
I'm have a solution using find and toArray, looping over documents in nodejs moving the field up one level, and insert the new documents in a new collection:
coll.find().toArray(function(err, documents) {
for (var doc in documents) {
newDocs.push(documents[doc].main);
}
newColl.insert(newDocs, function(err) {
...
});
});
I'm interested in a maybe more efficient/elegant solution.
If i understand, try this.
db.coll.aggregate([
{
$group:{
_id:"$_id",
case :{$first:"$main.case"},
csc :{$first:"$main.csc"},
}
},{
$out:"new_collection"
}
])
after that you have a new collection called "new_collection" with this format
{
"_id" : 3,
"case" : 4,
"csc" : 5
}
{
"_id" : 2,
"case" : 3,
"csc" : 4
}
UPDATE You Can try
db.new_collection.update({},{$rename:{'main.case':'case','main.csc':'csc'}},{multi:true})
//now is Empty
db.new_collection.update({},{$unset:{'main':1}},{multi:true})
if I understand :)