我有一个投影,涉及创建一个包装数组 (
facets
),并创建一个带有常量的字段 (name
)。
AggregationOperation aggregationOperation = context -> Document.parse(
"""
{
"$project": {
"products": 1,
"count": {
"$arrayElemAt": [
"$count.count",
0
]
},
"facets": [
{
"name": "PRODUCT_CATEGORY",
"items": "$productCategory"
}
]
}
}
""");
我尝试使用 Spring Data Mongo Aggregation 编写它,但找不到正确的代码来编写。第一部分有效,但对于
facets
部分则不然。
ProjectionOperation projectionOperation = project("products")
.and("count.count").arrayElementAt(0).as("count")
.and("facets").nested(bind("productCategory", "items"));
Document doc = new Document("name", "PRODUCT_CATEGORY").append("items", "$productCategory");
ProjectionOperation projectionOperation = Aggregation
.project("products")
.and("count.count").arrayElementAt(0).as("count")
.andArrayOf(doc).as("facets");