0

I have two mongoDB collections named users and users_custom. For example: users collection looks like:

{
    "_id" : ObjectId("53ac64d445fa47e97a5f3b50"),
    "user_id" : "1",
    "Name" : "Mr. A",
    "phone" : "12345"
}
{
    "_id" : ObjectId("53ac64e145fa47e97a5f3b53"),
    "user_id" : "2",
    "Name" : "Mr. B",
    "phone" : "23456"
}

users_custom collection looks like:

{
    "_id" : ObjectId("53ac64d445fa47e97a5f3b32"),
    "user_id" : "1",
    "Name" : "Mr. A Modified",
    "email" : "someone@gmail.com"
}
{
    "_id" : ObjectId("53ac64e145fa47e97a5f3b232"),
    "user_id" : "2",
    "Name" : "Mr. B",
    "address" : "some address"
}

I want to merge users_custom collection over users collection into users_final collection. That way it will looks like:

{
    "_id" : ObjectId("53ac64d445fa47e97a5f3b32"),
    "user_id" : "1",
    "Name" : "Mr. A Modified",
    "phone" : "12345"
    "email" : "someone@gmail.com"
}
{
    "_id" : ObjectId("53ac64e145fa47e97a5f3b232"),
    "user_id" : "2",
    "Name" : "Mr. B",
    "phone" : "23456"
    "address" : "some address"
} 

Any idea or example code will be greatly appreciated. Thanks in advance.

hasib32
  • 835
  • 1
  • 13
  • 31

1 Answers1

1

What you're trying to do is join two collections. MongoDB doesn't support joins natively though, but through the map reduce framework you can implement a join function. This is something that may take a while and should be done offline, not as a real-time query.

These questions/articles should provide some guidance:

MongoDB: Combine data from multiple collections into one..how?

Merging two collections in MongoDB

http://tebros.com/2011/07/using-mongodb-mapreduce-to-join-2-collections/

Community
  • 1
  • 1
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
  • Thanks for your quick reply. I saw your two links and it looks like i need to use MongoDB map reduce. Is map reduce will dynamic because I don't know which field will get updated or which new field will get added. – hasib32 Jun 26 '14 at 20:05
  • 1
    I don't quite follow. If you know what your 2 collections look like, you should know all of the fields that you want to combine, correct? Or do the documents have different fields? If that's the case, you should be able to get the field names during the map phase http://stackoverflow.com/questions/2298870/mongodb-get-names-of-all-keys-in-collection – Jeff Storey Jun 26 '14 at 20:10