I have many collections of documents in a mongo database that look like this:
{
"_id": ObjectId("4ee5e9079b14f74ef14ddd2f"),
"number": 456,
"date": "2012-02-13"
}
I need to rename the field "created_at" to "date" and I’m using Rockmongo. I think the best way to go about renaming this field would be to use the execute panel in Rockmongo. Basing my code off of this post I tried this:
function rename(x){
db_name.coll_name.update({"_id":x._id}, {
$rename: {"date":"created_at"}
});
}
db_name.coll_name.find({"date":{$ne:null}}).forEach(rename);
but have been unsuccessful. I have mainly worked with pymongo (and could do it with that), but I think learning this execute panel would be very helpful as I continue to use rockmongo.
If you want to do all of the documents in your collection, you can do it all at once with a multi update, like so:
Where “false” is the upsert option, and “true” is the multi-update option.
Hope that helps.
Updated with an example. It even works if the field is already partly renamed, or if the field doesn’t exist in all documents: