2015-12-09 40 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字段尚未爲他們創建。 –

相關問題