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

Viewed 2481

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 Answers

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

Related