Java 按投影字段分组时引用无效
我有两个文档:比如说Java 按投影字段分组时引用无效,java,mongodb,spring-data,spring-data-mongodb,Java,Mongodb,Spring Data,Spring Data Mongodb,我有两个文档:比如说Foo和Qux Foo如下所示: { "_id": ObjectId("5c52bb1af9b7bb512458a6d1"), "name": "Foo 1", "description": "This is a Foo", "bars": [ { "name": "Bar 1", "description": "This is a Bar", "qux"
Foo
和Qux
Foo
如下所示:
{
"_id": ObjectId("5c52bb1af9b7bb512458a6d1"),
"name": "Foo 1",
"description": "This is a Foo",
"bars": [
{
"name": "Bar 1",
"description": "This is a Bar",
"qux": ObjectId("5c3f3d59d45cca2d1860bb4e")
},
{
"name": "Bar 2",
"description": "This is a Bar",
"qux": ObjectId("5c3f3d59d45cca2d1860bb4e")
}
]
}
而Qux
看起来像:
{
"_id": ObjectId("5c3f3d59d45cca2d1860bb4e"),
"name": "Qux 1",
"description": "This is a Qux"
}
我的目标是将相应的Qux
嵌入Foo.bar
的每个元素,如下所示:
[{
"_id": ObjectId("5c52bb1af9b7bb512458a6d1"),
"name": "Foo 1",
"description": "This is a Foo",
"bars": [
{
"name": "Bar 1",
"description": "This is a Bar",
"qux": {
"_id": ObjectId("5c3f3d59d45cca2d1860bb4e"),
"name": "Qux 1",
"description": "This is a Qux"
}
},
{
"name": "Bar 2",
"description": "This is a Bar document",
"qux": {
"_id": ObjectId("5c3f3d59d45cca2d1860bb4e"),
"name": "Qux 1",
"description": "This is a Qux"
}
}
]
}]
我尝试了以下聚合:
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(Criteria.where("_id").is(id)),
Aggregation.unwind("bars", true),
Aggregation.lookup("qux", "bars.qux", "_id", "bars.qux"),
Aggregation.project("_id", "name", "description")
.and("bars.qux").arrayElementAt(0).as("bars.qux")
.and("bars.name").as("bars.name")
.and("bars.description").as("bars.description"),
Aggregation.group("_id")
.push("bars").as("bars")
.first("name").as("name")
.first("description").as("description")
);
但是由于这一行.push(“bar”).as(“bar”)
,它抛出了一个IllegalArgumentException
:
如果我在不执行组操作的情况下执行聚合,它会工作,但我会为每个bar元素获取一个Foo,并且每个Foo包含一个不同的bar元素,这是我所期望的,因为我将它们展开:
[{
"_id": ObjectId("5c52bb1af9b7bb512458a6d1"),
"name": "Foo 1",
"description": "This is a Foo",
"bars": {
"name": "Bar 1",
"description": "This is a Bar",
"qux": {
"_id": ObjectId("5c3f3d59d45cca2d1860bb4e"),
"name": "Qux 1",
"description": "This is a Qux"
}
}
},
{
"_id": ObjectId("5c52bb1af9b7bb512458a6d1"),
"name": "Foo 1",
"description": "This is a Foo",
"bars": {
"name": "Bar 2",
"description": "This is a Bar",
"qux": {
"_id": ObjectId("5c3f3d59d45cca2d1860bb4e"),
"name": "Qux 1",
"description": "This is a Qux"
}
}
}]
有什么方法可以实现我的目标吗?您可以在不使用
$unwind
的情况下获得所需的输出。一旦我们$lookup
我们就可以$map
qux rom通过$indexOfArray
和$arrayElemAt
加入数组,并使用$mergeObjects
合并对象
db.foo.aggregate([
{$lookup: {from : "qux", localField : "bars.qux", foreignField : "_id", as : "join"}},
{$addFields: {bars: {$map : {input : "$bars", as : "b", in : {$mergeObjects :[ "$$b", {qux: {$arrayElemAt : ["$join", {$indexOfArray: ["$join._id", "$$b.qux"]}]}}]}}}}},
{$project: {join:0}}
]).pretty()
输出
{
"_id" : ObjectId("5c52bb1af9b7bb512458a6d1"),
"name" : "Foo 1",
"description" : "This is a Foo",
"bars" : [
{
"name" : "Bar 1",
"description" : "This is a Bar",
"qux" : {
"_id" : ObjectId("5c3f3d59d45cca2d1860bb4e"),
"name" : "Qux 1",
"description" : "This is a Qux"
}
},
{
"name" : "Bar 2",
"description" : "This is a Bar",
"qux" : {
"_id" : ObjectId("5c3f3d59d45cca2d1860bb4e"),
"name" : "Qux 1",
"description" : "This is a Qux"
}
}
]
}
这是可行的,但我需要在java中用spring数据mongodb编写它。
{
"_id" : ObjectId("5c52bb1af9b7bb512458a6d1"),
"name" : "Foo 1",
"description" : "This is a Foo",
"bars" : [
{
"name" : "Bar 1",
"description" : "This is a Bar",
"qux" : {
"_id" : ObjectId("5c3f3d59d45cca2d1860bb4e"),
"name" : "Qux 1",
"description" : "This is a Qux"
}
},
{
"name" : "Bar 2",
"description" : "This is a Bar",
"qux" : {
"_id" : ObjectId("5c3f3d59d45cca2d1860bb4e"),
"name" : "Qux 1",
"description" : "This is a Qux"
}
}
]
}