Java Spring Mongo聚合项目筛选器
我在Mongo Shell中使用的投影上实现过滤器时遇到问题。我有一个人口普查对象,它包含一个员工列表Java Spring Mongo聚合项目筛选器,java,spring,mongodb,aggregation-framework,Java,Spring,Mongodb,Aggregation Framework,我在Mongo Shell中使用的投影上实现过滤器时遇到问题。我有一个人口普查对象,它包含一个员工列表 { "_id": "ID", "name": "census1", "employees": [ { "eeId": "EE_ID1" }, { "eeId": "EE_ID2" }, { "eeId": "EE_ID3" } } 实际上,这可能包含很多员工。因此,我希望能够检索主要的人口普查对象,以及员工
{
"_id": "ID",
"name": "census1",
"employees": [ {
"eeId": "EE_ID1"
},
{
"eeId": "EE_ID2"
},
{
"eeId": "EE_ID3"
}
}
实际上,这可能包含很多员工。因此,我希望能够检索主要的人口普查对象,以及员工的子集。我已经实现了'slice',所以这将根据员工的eeId检索一组员工
这很好:
db.census.aggregate(
[
{
$match: {
"_id": ObjectId("ID1")
}
},
{
$project: {
"censusName": 1,
"employees" : {
$filter : {
input: "$employees",
as: "employees",
cond: { $in: [ "$$employees.eeId", ["EE_ID1", "EE_ID3"]] }
}
}
}
}
]
).toArray()
问题是,我无法用Java实现它。这里的“employeeIds”是我想要的一组ID
MatchOperation matchCensusIdStage = Aggregation.match(new Criteria("id").is(censusId));
ProjectionOperation projectStage = Aggregation.project("censusName")
.and(Filter.filter("employees")
.as("employees")
.by(In.arrayOf(employeeIds).containsValue("employees.eeId")))
.as("employees");
Aggregation aggregation = Aggregation.newAggregation(matchCensusIdStage, projectStage);
return mongoTemplate.aggregate(aggregation, Census.class, Census.class).getMappedResults().get(0);
对此,不返回任何结果。我也尝试过用BasicDBObject实现它,但也遇到了问题
编辑(解决方法):
我确实使用聚合得到了一个解决方案,但没有在项目中使用过滤器。这就是我所做的:
db.parCensus.aggregate(
// Pipeline
[
{
$match: {
"_id": ObjectId("ID1")
}
},
{
$project: {
"_id": 0, "employee": "$employees"
}
},
{
$unwind: "$employee"
},
{
$match: {
"employee.eeId": { $in: ["EE_ID1", "EE_ID3"] }
}
}
]
).toArray()
Java代码:
MatchOperation matchCensusIdStage = Aggregation.match(new Criteria("id").is(censusId));
ProjectionOperation projectStage = Aggregation.project("censusName").and("employees").as("employee");
UnwindOperation unwindStage = Aggregation.unwind("employee");
MatchOperation matchEmployeeIdsStage = Aggregation.match(new Criteria("employee.eeId").in(employeeIds));
Aggregation aggregation = Aggregation.newAggregation(matchCensusIdStage, projectStage, unwindStage, matchEmployeeIdsStage);
我知道我可以在末尾添加一个$group,将其放回一个普查对象中,但我只是创建了一个单独的CensusEmployee对象来存储所有内容。问题帖子中发布的聚合查询工作正常。聚合
ArrayOperators.In中的MongoDB Spring数据API语法不清楚。我无法实现基于此聚合的解决方案(并且没有与网络相关的答案)
但是,另一种解决方案是基于以下聚合查询的,而且效果很好
db.collection.aggregate( [
{ $unwind: "$employees" },
{ $match: { "employees.eeId": { $in: ["EE_ID1", "EE_ID3"] } } },
{ $group: { _id: "$_id", name: { $first: "$name" }, employees: { $push: "$employees" } } }
] )
Java代码:
List<String> empsToMatch = Arrays.asList("EE_ID1", "EE_ID3");
MongoOperations mongoOps = new MongoTemplate(MongoClients.create(), "test");
Aggregation agg = newAggregation(
unwind("employees"),
match(Criteria.where("employees.eeId").in(empsToMatch )),
group("_id")
.first("name").as("name")
.push("employees").as("employees")
);
AggregationResults<Document> results = mongoOps.aggregate(agg, "collection", Document.class);
List emptosatch=Arrays.asList(“EE_ID1”、“EE_ID3”);
MongoOperations mongoOps=新的MongoTemplate(mongoClient.create(),“test”);
聚合agg=newAggregation(
放松(“员工”),
匹配(emptatch中的标准,其中(“employees.eeId”),
组(“\u id”)
.第一名(“姓名”)。作为(“姓名”)
.推送(“员工”)。作为(“员工”)
);
AggregationResults=mongoOps.aggregate(agg,“collection”,Document.class);
对,ArrayOperators.In是我试图找到解决方案的地方。我将用我最终得到的工作编辑我的问题,但我希望有人能帮助我如何使用ArrayOperators.In。