我在MongoDB 4.4.2中实现了以下聚合,它运行得很好:
[{
$match: {
"$expr": {
"$and": [{
"$not": "$history_to"
}, ],
},
}
}, {
$unwind: {
"path": "$used",
"preserveNullAndEmptyArrays": true,
}
}, {
$project: {
"ticket": "$$ROOT",
"status": {
"$map": {
"input": {
"$filter": {
"input": "$status",
"as": "cstatus",
"cond": {
"$not": "$$cstatus.history_to",
},
},
},
"as": "status",
"in": "$$status.value",
},
},
}
}]
但是,当我在MongoDB 4.4.4中尝试时,我会遇到input to $filter must be an array not long
。
如果有任何帮助的话,我想这一错误的原因与以下因素有关:
"cond": {
"$not": "$$cstatus.history_to",
},
因为当我评论$not
时,它工作得很好;起初,我认为可能不再支持$not
了,但是它是支持的,所以我没有想法了。
一些示例文档
{
"_id" : ObjectId("6083ca1ce151beea45602e5d"),
"created_at" : ISODate("2021-04-24T07:34:52.947Z"),
"ticket_id" : ObjectId("6083ca1c68badcedddd875ba"),
"expire_at" : ISODate("2021-04-24T19:30:00Z"),
"history_from" : ISODate("2021-04-24T07:34:52.992Z"),
"created_by" : ObjectId("604df7857d58ab06685ed02e"),
"serial" : "116627138",
"status" : [
{
"history_from" : ISODate("2021-04-24T07:34:52.985Z"),
"history_created_by" : ObjectId("604df7857d58ab06685ed02e"),
"value" : NumberLong(1)
}
],
"history_created_by" : ObjectId("604df7857d58ab06685ed02e")
},
{
"_id" : ObjectId("60713b0fe151beea45602e56"),
"created_by" : ObjectId("604df7857d58ab06685ed02e"),
"ticket_id" : ObjectId("60713b0f68badcedddd875b8"),
"created_at" : ISODate("2021-04-10T05:43:43.228Z"),
"history_created_by" : ObjectId("604df7857d58ab06685ed02e"),
"status" : [
{
"history_created_by" : ObjectId("604df7857d58ab06685ed02e"),
"value" : NumberLong(1),
"history_from" : ISODate("2021-04-10T05:43:43.277Z")
}
],
"serial" : "538142578",
"expire_at" : ISODate("2021-04-10T19:30:00Z"),
"history_from" : ISODate("2021-04-10T05:43:43.281Z"),}
发布于 2021-05-11 07:32:15
根据您可以检查游乐场的示例文档,您的查询看起来不错,
但是,当我在MongoDB 4.4.4中尝试时,我会遇到
$filter
的输入必须是一个不太长的数组。
这不是任何特定于MongoDB版本的问题,错误说提供了字段status
,因为$filter
中的输入不是数组类型,而是长类型,$filter
输入应该是数组类型。
在status
字段中肯定存在一些具有非数组值的文档。
如果要检查是否可以在$filter
操作之前匹配条件,
{ $match: { status: { $type: "array" } } } // 4 = "array"
这将通过status
过滤文档,它应该是数组类型。
https://stackoverflow.com/questions/67470640
复制相似问题