Copy a field from one collection to another in mongodb with foreign key as mixed type

ぐ巨炮叔叔 提交于 2019-12-24 17:47:41

问题


Although i have found a similar question on stackOverFlow MongoDB copy a field to another collection with a foreign key

I want to copy a field name from userdetails collection to user collection where userId in userDetails equals _id in user.

user collection

{                                                                                                                                                                                                            
  "_id" : ObjectId("5b97743bbff66e0be66283cc"),                                                                                                                                                       
  "username" : "mmi_superadmin",                                                                                                                                                                                     
  "accId" : "acc1"                                                                                                                                                                  
}                                                                                                                                                                                                    
{                                                                                                                                                                                                            
  "_id" : "c21d580ea3ca5c7a1664bd5feb57f0c8",                                                                                                                                                             
  "username" : "client",                                                                                                                                                                               
  "accId" : "acc1"                                                                                                                                                               
}  

userDetail collection

{
  "_id" : ObjectId("5b97743bbff66e0be66283cd"),                                                                                                                                                    
  "userId" : "5b97743bbff66e0be66283cc",                                                                                                                                                         
  "name" : "mmi_superadmin"                                                                                                                                                                    
}                                                                                                                                                                                                    
{                                                                                                                                                                                                            
  "_id" : "5bab8a60ef86bf90f1795c44",                                                                                                                                                                  
  "userId" : "c21d580ea3ca5c7a1664bd5feb57f0c8",                                                                                                                                                                                                                                                                                                                        
  "name" : "RAHUL KUMAR TIWARI"                                                                                                                                                                       
}    

Here is my query :

db.userDetails.find().forEach(
function(x) {
  db.user.update( {_id :x.userId}, {$set: {name:x.name}});
}
);

This query is partially working. It only updates user documents where _id is of type string. User document with _id as ObjectId are not getting updated.


回答1:


Please check your documents _id's (because in your example some _id's is not valid documents _id's. for example c21d580ea3ca5c7a1664bd5feb57f0c8 not a mongo _id) and use this query:

let usersIds = [];
db.user.find({"_id": {$type: 7}}).forEach(doc => {
    usersIds.push(doc._id + '')

    db.userDetail.find({
        userId: {
            $in: usersIds
        }
    }).forEach(doc => {
        db.user.update(
            {
                "_id": ObjectId(doc.userId)
            },
            {
                $set: {
                    "name": doc.name
                }
            },
            { 
                multi: false,
                upsert: false
            }
        )
    })

})

if you have any question feel free to ask



来源:https://stackoverflow.com/questions/53261010/copy-a-field-from-one-collection-to-another-in-mongodb-with-foreign-key-as-mixed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!