如何在mongodb的两个集合之间找到匹配元素? [英] How to find match elements in between two collections in mongodb?

查看:123
本文介绍了如何在mongodb的两个集合之间找到匹配元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mongodb数据库,但是我很少陷入一种逻辑,如何在mongodb的两个集合之间找到匹配元素.

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.

用户集合

[{
   "_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"
}]

联系人集合

[{
    "_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.

推荐答案

我猜您想做的是聚合+查找".像这样:

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: [] } }
   }
])

结果,您得到:

{
"_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"
    }
]
}

如果只想显示姓名和电子邮件,则必须添加{$ project:{name:1,email:1,_id:0}

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 }
])

然后您将获得: { "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"

}]
)

希望它能起作用!

有关更多信息,请 https://docs.mongodb.com/manual /reference/operator/aggregation/lookup/

这篇关于如何在mongodb的两个集合之间找到匹配元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆