假设我有以下文件
Article { Comment: embedMany }
Comment { Reply: embedMany }
Reply { email: string, ip: string }
我想创建一个选择不同Reply.ip
Reply.email = xxx
这样的东西,只是它不起作用..
db.Article.find("Comment.Reply.email" : "xxx").distinct("Comment.Reply.ip")
JSON导出:
{
"_id":{
"$oid":"4e71be36c6eed629c61cea2c"
},
"name":"test",
"Comment":[
{
"name":"comment test",
"Reply":[
{
"ip":"192.168.2.1",
"email":"yyy"
},
{
"ip":"127.0.0.1",
"email":"zzz"
}
]
},
{
"name":"comment 2 test",
"Reply":[
{
"ip":"128.168.1.1",
"email":"xxx"
},
{
"ip":"192.168.1.1",
"email":"xxx"
}
]
}
]
}
我跑:db.Article.distinct("Comment.Reply.ip",{"Comment.Reply.email" : "xxx"})
我希望:["128.168.1.1", "192.168.1.1"]
我:["127.0.0.1", "128.168.1.1", "192.168.1.1", "192.168.2.1"]
答案 0 :(得分:54)
Distinct
查询条件是这样的
db.Article.distinct("Comment.Reply.ip",{"Comment.Reply.email" : "xxx"})
不是其他方式
编辑:
我现在理解这个问题,为了匹配/过滤子文档,我们需要使用$ elemMatch运算符,就像这样
db.Article.distinct("Comment.Reply.ip",{Comment: {$elemMatch: {"Reply.email" : "xxx"}}})
但是如果子文档包含子数组(在您的情况下,您有回复数组),这将不起作用。已存在问题$elemMatch on subArray已打开。它计划用于mongo 2.1。您可以查看链接以获取更多信息
答案 1 :(得分:1)
也许你可以试试这个
// as you have it
UIStoryboard *mainsboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [mainsboard instantiateViewControllerWithIdentifier:@"notice"];
// but now build a navigation controller, too
UINavigationController *navVC = [mainsboard instantiateViewControllerWithIdentifier:@"noticeNavigationController"];
// make your vc the root
navVC.viewControllers = @[ vc ];
// and present** that navigation controller
[self.window.rootViewController presentViewController: navVC animated:YES completion:nil];
示例的结果应为
db.Article.aggregate([
{$unwind: "$Comment"},
{$unwind: "$Comment.Reply"},
{$match: {"Comment.Reply.email": "xxx"}},
{$group: {_id: "$Comment.Reply.ip"}}
])
答案 2 :(得分:0)
distinctdates=db.dailyreport_detailed.find(
{'uid':personemail,'month':searchdate2,'year':searchdate1}
)
.distinct('date_only')
找到的内容将根据条件进行检索,并且结尾处的不同将给出唯一的日期。
答案 3 :(得分:0)
db.collection.distinct("field_name", query)
将以数组的形式返回集合中所有不同的值
field_name 应该是您要为其返回不同值的字段。
query 指定要从中检索不同值的文档。例如:- {"Comment.Reply.email" : "xxx"}
或 {'country' : 'India','state' : 'MH','city' : 'mumbai'}