How to find match elements in between two collections in mongodb?

假装没事ソ 提交于 2019-12-12 03:36:27

问题


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": "Anshuman Pattnaik",
   "email": "anshuman@gmail.com",
   "number": "7022650603"
}, {
   "_id": "57cd53e6168df87ae2695544",
   "userid": "789456123",
   "name": "Deeptiman Pattnaik",
   "email": "deeptiman@gmail.com",
   "number": "7760941502"
}]

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": "Anshuman Pattnaik",
       "phonenumber": "7022650603",
       "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.


回答1:


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/




回答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);


来源:https://stackoverflow.com/questions/39330374/how-to-find-match-elements-in-between-two-collections-in-mongodb

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