假设我有以下文件
Article { Comment: embedMany }
Comment { Reply: embedMany }
Reply { email: string, ip: string }
Run Code Online (Sandbox Code Playgroud)
我想创建一个选择不同Reply.ip位置的查询Reply.email = xxx
像这样的东西,只有它不起作用..
db.Article.find("Comment.Reply.email" : "xxx").distinct("Comment.Reply.ip")
Run Code Online (Sandbox Code Playgroud)
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"
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
我跑: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"]
Ram*_*Vel 67
Distinct mongo中的查询条件是这样的
db.Article.distinct("Comment.Reply.ip",{"Comment.Reply.email" : "xxx"})
Run Code Online (Sandbox Code Playgroud)
不是其他方式
编辑:
我现在理解这个问题,为了匹配/过滤我们需要使用$ elemMatch运算符的子文档,就像这样
db.Article.distinct("Comment.Reply.ip",{Comment: {$elemMatch: {"Reply.email" : "xxx"}}})
Run Code Online (Sandbox Code Playgroud)
但如果子文档包含子数组(在您的情况下,您有回复数组),这将无效.在subArray上存在$ elemMatch存在的问题.它计划用于mongo 2.1.您可以查看链接以获取更多信息
db.collection.distinct("field_name", query)
将以数组的形式返回集合中所有不同的值
field_name应该是您想要返回不同值的字段。
查询指定要从中检索不同值的文档。前:-{"Comment.Reply.email" : "xxx"}或{'country' : 'India','state' : 'MH','city' : 'mumbai'}
| 归档时间: |
|
| 查看次数: |
51851 次 |
| 最近记录: |