2015-12-09 37 views
3

下面是数组联系人的模式。 contacts数组有一个字段hashtag,它是另一个数组。我如何找到所有具有特定标签的联系人?例如所有带有标签“zxc”的联系人?如何在MongoDB的数组中找到所有数组

"contacts" : [ 
    { 
     "addedDate" : ISODate("2015-12-02T09:06:09.891Z"), 
     "personEmailId" : "[email protected]", 
     "_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" : "[email protected]", 
     "_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" : "[email protected]", 
     "_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() ,它只返回一个联系人。我需要获得所有联系人。

+0

你在这里使用哪个建模包?猫鼬? – Rodion

+0

是的,我正在使用猫鼬。 –

回答

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"}}} 
]) 
+0

工程就像一个魅力。谢谢。 –

0

您可以使用

对于多个主题标签在单文档

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

对于单包括hashtag

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

请参阅$in

+0

这将返回所有文档。即使没有标签。 –

+0

没有hashtags意味着没有hashtags属性的文档?以上将不会返回这些文件!请解释? –

+0

它返回没有hashtag“zxc”或没有hashtag属性的所有联系人。因为某些联系人没有任何hashtags,因此hashtag字段尚未为他们创建。 –

相关问题