Spring Mongo 聚合项目过滤器

我在在 Mongo Shell 中工作的投影上实施过滤器时遇到问题.我有一个 Census 对象,其中包含一个员工列表.

I'm having an issue implementing a Filter on a Projection that I have working in the Mongo Shell. I've got a Census object that contains a list of Employees.

{
   "_id": "ID",
   "name": "census1",
   "employees": [ {
      "eeId": "EE_ID1"
   }, 
   {
      "eeId": "EE_ID2"
   },
   {
      "eeId": "EE_ID3"
   }
}

实际上,这可能包含很多员工.所以我希望能够检索主要的人口普查对象和一部分员工.我已经实现了切片",所以这将通过他们的 eeId 检索一组员工.

Realistically this could contain a lot of employees. So I'd like to be able to retrieve the main Census object, and a subset of employees. I've already implemented 'slice', so this is going to be retrieving a set of employees by their 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 字符串.

The problem is, I can't get it implemented in Java. Here 'employeeIds' is a String of the IDs I want.

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 来实现它,但也卡在那里.

For this, no results are returned. I've also tried implementing it with a BasicDBObject but got stuck there too.

编辑(解决方法):我确实得到了一个使用聚合但没有使用项目过滤器的解决方案.这就是我所做的:

EDIT (workaround): I did get a solution using aggregation but not with the filter on the project. This is what I did:

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 以将其放回一个 Census 对象,但我只是创建了一个单独的 CensusEmployee 对象来存储它.

I know I could add a $group at the end to put it back into one Census object, but I just created a separate CensusEmployee object to store it all.

推荐答案

问题帖子中发布的聚合查询工作正常.聚合 ArrayOperators.In 语法的 MongoDB Spring Data API 不清楚.我无法实现基于此聚合的解决方案(网上也没有相关答案).

The aggregation query posted in the question post works fine. The MongoDB Spring Data API for the aggregation ArrayOperators.In syntax is not clear. I couldn't implement a solution based on this aggregation (and no answers related to on the net).

但是,替代解决方案基于以下聚合查询 - 它工作正常.

But, the alternative solution is based on the following aggregation query - and it works fine.

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);

相关文章