元素让我们假设我有两个以下MongoDB的文件:
{
"_id" : ObjectId(a59b0e19aff8a2ed2128bef97"),
"name" : "John",
"matches" : [
{
"name" : "Bob",
"count" : 10
},
{
"name" : "John",
"count" : 20
},
{
"name" : "Alice",
"count" : 30
}
]
}
{
"_id" : ObjectId(b59b0e19aff8a2ed2128bef97"),
"name" : "Mike",
"matches" : [
{
"name" : "Bob",
"count" : 10
},
{
"name" : "John",
"count" : 20
},
{
"name" : "Alice",
"count" : 30
}
]
}
我需要建立的MongoDB查询,将比较“名”与“matches.name”。
在我的示例中,只有第一个文档才会匹配,因为“John”位于“matches”数组中。 由于Mike不在阵列中,第二个文档将不匹配。我试过{$where : "this.matches.name == this.name"}
但这不起作用。
任何想法?
你想蒙戈查询或JS代码会做什么?因为如果你已经在一些变量中加载了文件,一个js代码会更合适 – Prasanna
不,我想mongo查询。 JS解决方案在这种情况下很简单,当文件已经加载。 –