0

Below is my user collection data

user collection

{
    "_id" : ObjectId("584bc9ba420a6b189c510af6"),
    "old_user_id" :1,
    "name" :"aaa"
},
{
    "_id" : ObjectId("9ba420a584bc6b189c59ba42"),
    "old_user_id" : 2,
     "name" :"bbb"
},
{
    "_id" : ObjectId("59ba4284bc0a6b189c3323w23"),
    "old_user_id" : 3,
    "name" :"ccc"
}

myprofile collection

{        
    "old_user_id" :1,
    "name" :"aaa",
    "number":"123456789"
},
{
    "old_user_id" : 2,
     "name" :"bbb",
     "number":"678912345"
},
{
    "old_user_id" : 3,
    "name" :"ccc",
    "number":"673458912"    
},
{

    "old_user_id" : 2,
    "name" : "bbb",
    "adress" : "afsfdidhddk"
}   

My expectation:

I need to match old_user_id in both collections and update the user collection '_id' in my profile collection

{
    "userid":"584bc9ba420a6b189c510af6",
    "old_user_id" :1,
    "name" :"aaa",
    "number":"123456789"
},
{
    "userid":"9ba420a584bc6b189c59ba42",
    "old_user_id" : 2,
     "name" :"bbb",
     "number":"678912345"
},
{
    "userid":"59ba4284bc0a6b189c3323w23",
    "old_user_id" : 3,
    "name" :"ccc",
    "number":"673458912"
},
    {

    "old_user_id" : 2,
    "name" : "bbb",
    "adress" : "afsfdidhddk"
    "userid" : "9ba420a584bc6b189c59ba42"
}
its me
  • 524
  • 6
  • 28
  • 62

2 Answers2

2

You can use mongo aggregation. Use of $lookup, $project and $unwind will help. I have formulated the query, hope this helps:

db.myprofile.aggregate([
    {
      $lookup:
        {
          from: "user",
          localField: "old_user_id",
          foreignField: "old_user_id",
          as: "inventory_docs"
        }
   },
   { $project : { _id : "$inventory_docs._id",
                  old_user_id:"$old_user_id",
                 "name":"$name",
                 "number":"$number" } },
   {$unwind: "$_id"}
])
user1211
  • 1,507
  • 1
  • 18
  • 27
  • http://stackoverflow.com/questions/41124705/based-on-value-how-to-create-new-document-using-node-js-mongodb – its me Dec 14 '16 at 07:04
1

Update:

db.user.find().forEach(function (user) {

    var cursor = db.myprofile.find({"old_user_id":  user.old_user_id});

    cursor.forEach(function(myprofile) {
        myprofile.userid = user._id.str;
        db.myprofile.save(myprofile);
    });
});

Result:

db.myprofile.find().pretty()

   {
    "_id" : ObjectId("584e7294678ae15db4fab039"),
    "old_user_id" : 1,
    "name" : "aaa",
    "number" : "123456789",
    "userid" : "584e7294678ae15db4fab035"
}
{
    "_id" : ObjectId("584e7294678ae15db4fab03a"),
    "old_user_id" : 2,
    "name" : "bbb",
    "number" : "678912345",
    "userid" : "584e7294678ae15db4fab036"
}
{
    "_id" : ObjectId("584e7294678ae15db4fab03b"),
    "old_user_id" : 3,
    "name" : "ccc",
    "number" : "673458912",
    "userid" : "584e7294678ae15db4fab037"
}
{
    "_id" : ObjectId("584e7294678ae15db4fab03c"),
    "old_user_id" : 2,
    "name" : "bbb",
    "adress" : "afsfdidhddk",
    "userid" : "584e7294678ae15db4fab036"
}

Note: the _id fiels still appears in the resulting documents, but you should be able to live with that. Is a default behaviour of MongoDB. You can create a collection with no indexed _id field like in here: db.createCollection("user", { autoIndexId: false })

sergiuz
  • 5,353
  • 1
  • 35
  • 51
  • check my question if i have "old_user_id" : 2 multiple times its not working – its me Dec 12 '16 at 09:34
  • Indeed, corrected. Now it should work for more documents having the same `old_user_id`. – sergiuz Dec 12 '16 at 09:51
  • Please post other question if is not related to this one. – sergiuz Dec 12 '16 at 10:12
  • What is your next question? please post another question, and if I can help I will. Tough JS is not my strong point fo expertise. – sergiuz Dec 12 '16 at 15:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130426/discussion-between-its-me-and-sergiu-zaharie). – its me Dec 12 '16 at 15:58
  • http://stackoverflow.com/questions/41124705/based-on-value-how-to-create-new-document-using-node-js-mongodb – its me Dec 14 '16 at 07:05