【问题标题】:How to find all in an array of array in MongoDB如何在MongoDB中的数组数组中查找所有内容
【发布时间】:2016-03-14 14:59:32
【问题描述】:

以下是数组联系人的架构。联系人数组有一个字段标签,它是另一个数组。如何找到具有特定主题标签的所有联系人?例如所有带有标签“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。

标签: arrays node.js mongodb


【解决方案1】:

你可以通过聚合框架做到这一点:

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

2) 展开联系人字段。

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

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

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

【讨论】:

  • 像魅力一样工作。谢谢。
【解决方案2】:

你可以使用

单个文档中的多个主题标签

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

【讨论】:

  • 这将返回所有文档。即使没有主题标签。
  • 没有主题标签意味着文档没有主题标签属性?以上将不返回那些文件!请解释一下?
  • 它返回所有没有主题标签“zxc”或没有主题标签属性的联系人。因为一些联系人没有任何主题标签,因此尚未为他们创建主题标签字段。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-04
  • 1970-01-01
  • 2020-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-13
相关资源
最近更新 更多