以下是阵列联系人的架构。 contacts数组有一个字段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()
只返回了一个联系人。我需要得到所有联系人。
答案 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"}}}
])
答案 1 :(得分: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