db.programs_bak.find({}).forEach(function(doc) {
db.programs.update( { _id: doc._id },
{$set : { "showed" : doc.showed._i}});
});
Update MongoDB objects using existing objects
-
21-06-2023 - |
题
I'm trying to convert my mongodb data into more simpler format, but I don't know how to check if certain field is an array and update those only.
I have this kind of objects in my database currently
{ "_id" : ObjectId("xyz"), "name" : "Yeti ", "channel" : "ABC", "showed" : { "_isAMomentObject" : true, "_i" : "25.3.2014 23:40", ... }}
I also has some rows which are in new format already:
{ "_id" : ObjectId("xyz"), "name" : "Yeti ", "channel" : "ABC", "showed" : "25.3.2014 23:40" }
I would like to update all my objects which has array type in "showed" property into an object which has showed._i in showed property.
showed._i => showed, for all objects which have array in showed property.
I tried to do this update using my backup collection, but it put null into showed property for all objects:
db.programs_bak.find({}).forEach(function(doc) { db.programs.update( { _id: doc._id }, { showed: doc.showed._i },{ }); });
解决方案
不隶属于 StackOverflow