我正在做一个相当复杂的聚合管道,并且有一个相当奇怪的现象 - 我在这里提取了一个简短的示例来可视化我的问题。
它似乎与 MongoDb $addFields and $match 有关- 但它不包含任何信息供我解决手头的问题。
注意:请注意,我的问题不在于使用日期字段和/或处理值的具体示例,问题在于我无法$match
使用表达式 - 使用之前通过 $addFields
添加或不添加的字段。
给定 MongoDB:3.6.3(当前最新)
让我们插入一些测试数据:
db.testexample.insert({
"dateField": new ISODate("2016-05-18T16:00:00Z")
});
db.testexample.insert({
"dateField": new ISODate("2018-05-18T16:00:00Z")
});
现在让我们创建一个简单的管道,仅计算日期的年份和 $matches:
db.testexample.aggregate([
{
"$addFields": {
"dateFieldYear": {"$year": "$dateField"}
}
},
{
"$match": {
"dateFieldYear": {"$eq": "$dateFieldYear"}}
}
}
])
--> 没有匹配项
它应该匹配,因为它是相同的字段?也许有更多的技巧(使用 $add
)?
db.testexample.aggregate([
{
"$addFields": {
"dateFieldYear": {"$year": "$dateField"}
}
},
{
"$match": {
"dateFieldYear": {"$eq": {"$add": ["$dateFieldYear", 0]}}
}
}
])
--> 没有匹配项
仍然没有骰子..接下来我认为变量完全是一个问题。因此,让我们修复这些值:
db.testexample.aggregate([
{
"$addFields": {
"dateFieldYear": {"$year": "$dateField"}
}
},
{
"$match": {
"dateFieldYear": {"$eq": {"$add": [2016, 0]}}
}
}
])
--> 没有匹配项
等等..这里真的出了问题..让我们看看静态值:
db.testexample.aggregate([
{
"$addFields": {
"dateFieldYear": {"$year": "$dateField"}
}
},
{
"$match": {
"dateFieldYear": 2016
}
}
])
--> 找到 1 条记录!
所以我的结论似乎是$match
不能在聚合管道中的字段上使用表达式。但这似乎不可能 - 因为文档指出 $match
遵循查询语法 as described here .
任何人都可以帮助如何使用简单的示例来完成 $match
"dateFieldYear": {"$eq": "$dateFieldYear"}}
- 为什么这不符合预期吗?
非常感谢您的帮助
最佳答案
您可以使用$expr
(3.6 mongo版本运算符)在常规查询中使用聚合函数。
比较 query operators
与 aggregation comparison operators
.
就你的情况
db.testexample.find({$expr:{$eq:["$dateFieldYear", "$dateFieldYear"]}})
常规查询:
db.testexample.find({$expr:{$eq:["$dateFieldYear", {"$year": "$dateField"}]}})
聚合查询:
db.testexample.aggregate({$match:{$expr:{$eq:["$dateFieldYear", {"$year": "$dateField"}]}})
关于MongoDB聚合管道: $match with expression not possible?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49278288/