2

I am working on mongodb database, but i am little stuck in one logic, how do i find match elements in between two collections in mongodb.

Users Collection

[{
   "_id": "57cd539d168df87ae2695543",
   "userid": "3658975589",
   "name": "John Doe",
   "email": "johndoe@gmail.com",
   "number": "123654789"
}, {
   "_id": "57cd53e6168df87ae2695544",
   "userid": "789456123",
   "name": "William Rust",
   "email": "williamrust@gmail.com",
   "number": "963258741"
}]

Contacts Collection

[{
    "_id": "57cd2f6c3966037787ce9550",
    "contact": [{
       "id": "457899979",
       "fullname": "Abcd Hello",
       "phonenumber": "123575784565",
       "currentUserid": "123456789"
    }, {
       "id": "7994949849",
       "fullname": "Keyboard Mouse",
       "phonenumber": "23658974262",
       "currentUserid": "123456789"
    }, {
       "id": "7848848885",
       "fullname": "John Doe",
       "phonenumber": "852147852",
       "currentUserid": "123456789"
    }]
}]

So i want to find (phone number) matched elements from these two collections and list out those elements with their name and email.

Please kindly go through my post and suggest me some solution.

deeptimancode
  • 1,139
  • 4
  • 18
  • 40

2 Answers2

4

I'm guessing that you want to do is "aggregate + lookup". Something like this:

db.users.aggregate([{$lookup:
  {
        from: "contacts",
        localField: "number",
        foreignField: "phonenumber",
        as: "same"
    }
   },
   {
      $match: { "same": { $ne: [] } }
   }
])

As a result you get:

{
"_id" : "57cd539d168df87ae2695543",
"userid" : "3658975589",
"name" : "Anshuman Pattnaik",
"email" : "anshuman@gmail.com",
"number" : "7022650603",
"same" : [
    {
        "_id" : ObjectId("5b361b864aa5144b974c9733"),
        "id" : "7848848885",
        "fullname" : "Anshuman Pattnaik",
        "phonenumber" : "7022650603",
        "currentUserid" : "123456789"
    }
]
}

If you want show only the name and the email, you have to add { $project: { name: 1, email:1, _id:0 }

db.users.aggregate([{$lookup:
  {
        from: "contacts",
        localField: "number",
        foreignField: "phonenumber",
        as: "same"
    }
   },
   {
      $match: { "same": { $ne: [] } }
   },
{ $project: { name: 1, email:1, _id:0 }
])

Then you'll get: { "name" : "Anshuman Pattnaik", "email" : "anshuman@gmail.com" }

For this to work you have to correct the insert of your contacts like this:

db.contacts.insert(

[{
   "id": "457899979",    
   "fullname": "Abcd Hello",
   "phonenumber": "123575784565",
   "currentUserid": "123456789"

}, {
   "id": "7994949849",
   "fullname": "Keyboard Mouse",
   "phonenumber": "23658974262",
   "currentUserid": "123456789"

}, {
   "id": "7848848885",
   "fullname": "Anshuman Pattnaik",
   "phonenumber": "7022650603",
   "currentUserid": "123456789"

}]
)

Hope it works!

For more information https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/

Jofe
  • 2,489
  • 1
  • 12
  • 12
2

it's not your complete answer, but it may help you to solve your problem.

you can compare two documents using below function. for more details see this answer

var compareCollections = function(){
        db.users collection.find().forEach(function(obj1){
            db.contacts collection.find({/*if you know some properties, you can put them here...if don't, leave this empty*/}).forEach(function(obj2){
                var equals = function(o1, o2){
                    // some code.
                };

                if(equals(ob1, obj2)){
                    // Do what you want to do
                }
            });
        });
    };

    db.eval(compareCollections);
Community
  • 1
  • 1
yash
  • 2,101
  • 2
  • 23
  • 32