简体   繁体   English

如何在MongoDB中查找数组中的所有数组

[英]How to find all in an array of array in MongoDB

Below is the schema for an array contacts. 下面是阵列联系人的架构。 The contacts array has a field hashtag which is another array. contacts数组有一个字段hashtag,它是另一个数组。 How do I find all the contacts which have a particular hashtag? 如何查找具有特定主题标签的所有联系人? For example all contacts with hashtag "zxc"? 例如所有带标签“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")
    }

I did this query - db.myColl.find({"contacts.hashtag":"zxc"},{"contacts":{$elemMatch:{"hashtag":"zxc"}}}).pretty() which returned only one contact. 我做了这个查询 - db.myColl.find({"contacts.hashtag":"zxc"},{"contacts":{$elemMatch:{"hashtag":"zxc"}}}).pretty()返回只有一个联系人。 I need to get all contacts. 我需要得到所有联系人。

You can do it via aggregation framework: 您可以通过聚合框架来完成:

1) Do an initial match to reduce the input set for aggregation 1)进行初始匹配以减少聚合的输入集

2) Unwind contacts field. 2)展开联系人字段。

3) Filter out contacts with hashtag 'zxc' 3)使用#zxc标签过滤掉联系人

4) Group by id and put contacts on "contacts" field. 4)按ID分组并将联系人放在“联系人”字段中。

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

You can use 您可以使用

For multiple hashtag in single doc 对于单个doc中的多个hashtag

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

For single hashtag 对于单个主题标签

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

Please refer $in 请参考$ in

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM