Spring数据MongoTemplate聚合错误'input to $filter must be an array not object'
Spring Data MongoTemplate aggregation error 'input to $filter must be an array not object'
我有这样的文档结构:
{
"_id" : ObjectId("...."),
"oneMoreId" : "....",
"items" : [
{
"itemId" : "...",
"type" : "Food",
}
]
}
当我在运行JSON查询mongodb时:
db.inventory.aggregate([
{$match: { $and: [{"oneMoreId":"..."},{"items.type": "Food"}]}},
{"$project": {
"oneMoreId": 1,
"items": {
"$filter": {
"input": "$items",
"as": "item",
"cond": {
"$eq": ["$$item.type", "Food"]
}
}
}
}}
])
它工作得很好。
但是当我使用 Spring Data 的 MongoTemplate 进行 运行 聚合时,它抛出我
input to $filter must be an array not object
这是我的聚合查询(只是投影部分):
ProjectionOperation projection = project("oneMoreId").and(new AggregationExpression() {
@Override
public Document toDocument(AggregationOperationContext context) {
return new Document("$filter", new Document(
"input", "$items")
.append("as","item")
.append("cond", new Document("$eq", Arrays.asList("$$item.type","Food")))
);
}
}).as("items");
我在控制台打印出来,查询和上面的JSON查询完全一样。精确的。我什至尝试过纯 Spring 数据的查询:
ProjectionOperation projection = project("oneMoreId")
.and(filter("items")
.as("item")
.by(valueOf("item.type")
.equalToValue("Food"))).as("items");
同样,同样的错误(即使打印它会导致与上面完全相同的 JSON 查询)。包含项目的 java 对象是一个列表。我改成只是数组Item[],还是不行。
如有任何帮助,我们将不胜感激。
没关系,
有人(我)错误地把一个Item以对象的形式而不是数组的形式放入了。那把一切都搞砸了。刚刚删除了整条记录,一切正常。
我有这样的文档结构:
{
"_id" : ObjectId("...."),
"oneMoreId" : "....",
"items" : [
{
"itemId" : "...",
"type" : "Food",
}
]
}
当我在运行JSON查询mongodb时:
db.inventory.aggregate([
{$match: { $and: [{"oneMoreId":"..."},{"items.type": "Food"}]}},
{"$project": {
"oneMoreId": 1,
"items": {
"$filter": {
"input": "$items",
"as": "item",
"cond": {
"$eq": ["$$item.type", "Food"]
}
}
}
}}
])
它工作得很好。
但是当我使用 Spring Data 的 MongoTemplate 进行 运行 聚合时,它抛出我
input to $filter must be an array not object
这是我的聚合查询(只是投影部分):
ProjectionOperation projection = project("oneMoreId").and(new AggregationExpression() {
@Override
public Document toDocument(AggregationOperationContext context) {
return new Document("$filter", new Document(
"input", "$items")
.append("as","item")
.append("cond", new Document("$eq", Arrays.asList("$$item.type","Food")))
);
}
}).as("items");
我在控制台打印出来,查询和上面的JSON查询完全一样。精确的。我什至尝试过纯 Spring 数据的查询:
ProjectionOperation projection = project("oneMoreId")
.and(filter("items")
.as("item")
.by(valueOf("item.type")
.equalToValue("Food"))).as("items");
同样,同样的错误(即使打印它会导致与上面完全相同的 JSON 查询)。包含项目的 java 对象是一个列表。我改成只是数组Item[],还是不行。
如有任何帮助,我们将不胜感激。
没关系,
有人(我)错误地把一个Item以对象的形式而不是数组的形式放入了。那把一切都搞砸了。刚刚删除了整条记录,一切正常。