比方說,我有以下文件mongoDB不同&在相同的查詢?
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"]
有我給予好評的「我跑」,「我期待「,」我得到「,如果只有格式化的所有問題都是這樣! – adelriosantiago
這是一個解決方案的變種 http://stackoverflow.com/a/13864518/358013 – blueskin