如何在MongoDB中查找嵌套数组中的所有元素

3
以下是一个名为contacts的数组的模式。该contacts数组有一个名为hashtag的字段,它又是一个数组。如何查找所有具有特定hashtag的联系人?例如,所有带有hashtag“zxc”的联系人?
下面是一个名为contacts的数组的架构。 contacts数组有一个名为hashtag的字段,它是另一个数组。如何找到具有特定hashtag的所有联系人?例如,所有具有hashtag“zxc”的联系人?
"contacts" : [
    {
        "addedDate" : ISODate("2015-12-02T09:06:09.891Z"),
        "personEmailId" : "tell.fadgfdg@gmail.com",
        "_id" : ObjectId("565eb481bf35eeb83d7f9f13"),
        "verified" : true,
        "favorite" : true,
        "linkedinUserName" : null,
        "facebookUserName" : null,
        "twitterUserName" : "IamlifePaul",
        "count" : 2,
        "relationshipStrength_updated" : 0,
        "contactRelation" : {
            "decisionmaker_influencer" : null,
            "prospect_customer" : "prospect"
        },
        "source" : "abc",
        "mobileNumber" : "3546789",
        "skypeId" : "123",
        "designation" : "test",
        "companyName" : "Something",
        "location" : "Hyderabad, Telangana, India",
        "personName" : "Naveen Paul",
        "personId" : "565022d7dbeaeb9e17fc7083",
        "hashtag" : [

            "latestTag",
            "anotherTag",
            "#hash",
            "openLove",
            "hellTwo",
            "working?",
            "hello",
            "lol",
            "zxc"
        ],
        "lastInteracted" : ISODate("2015-12-08T05:07:53.746Z")
    },
{
        "addedDate" : ISODate("2015-12-02T09:06:09.891Z"),
        "personEmailId" : "naveenpaul.fadgfdg@gmail.com",
        "_id" : ObjectId("565eb481bf35eeb83d7f9f13"),
        "verified" : true,
        "favorite" : true,
        "linkedinUserName" : null,
        "facebookUserName" : null,
        "twitterUserName" : "IamlifePaul",
        "count" : 2,
        "relationshipStrength_updated" : 0,
        "contactRelation" : {
            "decisionmaker_influencer" : null,
            "prospect_customer" : "prospect"
        },
        "source" : "abc",
        "mobileNumber" : "3546789",
        "skypeId" : "123",
        "designation" : "test",
        "companyName" : "Something",
        "location" : "Hyderabad, Telangana, India",
        "personName" : "Naveen Paul",
        "personId" : "565022d7dbeaeb9e17fc7083",
        "hashtag" : [

            "latestTag",
            "anotherTag",
            "#hash",
            "openLove",
            "hellTwo",
            "working?",
            "hello",
            "lol",
            "zxc"
        ],
        "lastInteracted" : ISODate("2015-12-08T05:07:53.746Z")
    },
{
        "addedDate" : ISODate("2015-12-02T09:06:09.891Z"),
        "personEmailId" : "naveenpaul.fadgfdg@gmail.com",
        "_id" : ObjectId("565eb481bf35eeb83d7f9f13"),
        "verified" : true,
        "favorite" : true,
        "linkedinUserName" : null,
        "facebookUserName" : null,
        "twitterUserName" : "IamlifePaul",
        "count" : 2,
        "relationshipStrength_updated" : 0,
        "contactRelation" : {
            "decisionmaker_influencer" : null,
            "prospect_customer" : "prospect"
        },
        "source" : "abc",
        "mobileNumber" : "3546789",
        "skypeId" : "123",
        "designation" : "test",
        "companyName" : "Something",
        "location" : "Hyderabad, Telangana, India",
        "personName" : "Naveen Paul",
        "personId" : "565022d7dbeaeb9e17fc7083",
        "hashtag" : [

            "polly",
            "tagger",
            "#hash",
            "working?",
            "hello",
            "lol",
            "zxc"
        ],
        "lastInteracted" : ISODate("2015-12-08T05:07:53.746Z")
    }

我执行了这个查询 - db.myColl.find({"contacts.hashtag":"zxc"},{"contacts":{$elemMatch:{"hashtag":"zxc"}}}).pretty(),只返回了一个联系人。我需要获取所有的联系人。


你在这里使用哪个建模包?mongoose吗? - Rodion
是的,我正在使用Mongoose。 - Naveen Paul
2个回答

1

您可以通过聚合框架来完成:

1)进行初始匹配以减少聚合的输入集

2)展开联系人字段。

3)过滤掉带有“zxc”标签的联系人

4)按id分组并将联系人放在“contacts”字段中。

db.getCollection('contacts').aggregate([
    {$match: {"contacts.hashtag":"zxc"}},
    {$unwind: "$contacts"},
    {$match: {"contacts.hashtag":"zxc"}},
    {$group: {_id:"$_id", "contacts": {$push:"$contacts"}}}
])

非常好用。谢谢。 - Naveen Paul

0

你可以使用

在单个文档中使用多个哈希标签

db.myColl.find({"contacts.hashtag":{$in : ['zxc','anotherTag']}});
//will return all doc with any of the two hashtags.

针对单个哈希标签

   db.myColl.find({"contacts.hashtag":'zxc'});
  //return all documents with hashtag 'zxc'

请参考$in


这将返回所有文档,即使没有标签。 - Naveen Paul
没有标签意味着一个没有标签属性的文档吗?上面的代码不会返回那些文档!!请解释一下? - sahil gupta
它会返回所有没有“zxc”标签或没有标签属性的联系人。因为有些联系人没有任何标签,所以他们的标签字段尚未创建。 - Naveen Paul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接