来自$elementMatch的MongoDB文档:
$ elemMatch运算符匹配包含数组字段的文档,其中至少有一个元素匹配所有指定的查询条件 .
But how can I match documents that contain an array field with ALL elements that match the query?
例如,我有这样的文件:
{
"_id": ObjectId("55c99649b8b5fc5b0a2f1c83"),
"sku": "ed-39211",
"created_at": ISODate("2015-08-11T06:29:29.139+0000"),
"formats": [{
"name": "thefile",
"_id": ObjectId("55c99649f2e2d6353348ec9c"),
"prices": [{
"price": 4.49,
"currency": "GBP",
"territory": "GB",
"_id": ObjectId("55c99649f2e2d6353348ec9f")
}, {
"price": 6.99,
"currency": "USD",
"territory": "US",
"_id": ObjectId("55c99649f2e2d6353348ec9e")
}, {
"price": 6.99,
"currency": "CHF",
"territory": "CH",
"_id": ObjectId("55c99649f2e2d6353348ec9d")
}]
}]
}
我需要匹配 all formats.prices.price
> 5的所有文档
如果我使用以下查询:
{ 'formats.prices': { $elemMatch: { price: { $gte: 5 } } } }
该文件将匹配,因为至少有一个价格> 5
我也试过这个,但它似乎不起作用:
{ 'formats.prices': { $all: { $elemMatch: {price: { $gte: 0.98 } } } } }
有没有办法排除该文件,查看所有价格至少不是一个?
2 回答
找到了!这很简单,只需使用
$not
运算符并检查相反(<5):您可以使用Aggegation或MAP REDUCE来实现它:
First solution is using Map-Reduce :
我创建了一个名为“format”的集合并插入到数据下面:
Map_reduce :
输出:
Second Solution using Aggregation :
使用聚合运算符$ unwind和$ size我们可以使用以下查询获得所需的结果:
在"Formats"和"Formats.prices"的$unwind之后,"Formats.prices"的大小被采用,然后"prices"上的$match完成,并且再次为"Formats.prices"计算新大小 .
如果大小相同,则“格式”字段中的所有“价格”都大于5,并且将投影文档 .
输出: