【发布时间】:2017-09-22 17:33:45
【问题描述】:
假设我有以下两个 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 查询,将“name”与“matches.name”进行比较。
在我的示例中,只有第一个文档会被匹配,因为“John”在“matches”数组中。 第二个文档将不匹配,因为 Mike 不在数组中。
我试过{$where : "this.matches.name == this.name"},但这不起作用。
有什么想法吗?
【问题讨论】:
-
你想要mongo查询还是js代码可以?因为如果你已经在某个变量中加载了文档,那么js代码会更合适
-
不,我想要 mongo 查询。在这种情况下,当文档已经加载时,JS 解决方案很简单。
标签: javascript arrays mongodb