0

我正在合并来自两个集合projectscard_types聚合管道的记录,然后尝试cardFields从结果中删除合并记录的字段。所以这个想法是从汇总结果来看

{
   "projectId": "foo",
   ...
   "cardTypes": [
     {
        "itemId": "bar",
        "itemType": "Defect",
        "cardFields": [ { ... more objects here ...} ]
     },
     {
        ... more card types ...
     }
   ],
}

进入

{
   "projectId": "foo",
   ...
   "cardTypes": [
     {
        "itemId": "bar",
        "itemType": "Defect"
        ...
     },
     {
        ... more card types ...
     }
   ],
}

我写的方法是

public Flux<Document> getProjectDetails(String enterpriseId, String projectId) {
    AggregationOperation unset = UnsetOperation.unset("cardTypes.cardFields");
    return mongoOperations.aggregate(
            Aggregation.newAggregation(match(where(accountIdKeyName).is(enterpriseId).and(ownerIdKeyName).is(projectId)),
                    lookup("card_types", accountIdKeyName, accountIdKeyName, "cardTypes"),
                    unset), "project", Document.class);
}

该方法因异常而失败

org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 40324 (Location40324): 'Unrecognized pipeline stage name: '$unset''

4

1 回答 1

0

更改了方法实现以改为$project使用

public Flux<Document> getProjectDetails(String enterpriseId, String projectId) {
    List<AggregationOperation> pipeline = new ArrayList<>();
    pipeline.add(match(where(accountIdKeyName).is(enterpriseId).and(itemIdKeyName).is(projectId)));
    pipeline.add(context ->
            new Document("$lookup", new Document("from", "card_types")
                    .append("localField", "itemId")
                    .append("foreignField", "projectData.boardId")
                    .append("as", "cardTypes")
            ));
    pipeline.add(context ->
            new Document("$lookup", new Document("from", "swim_lanes")
                    .append("localField", "itemId")
                    .append("foreignField", "ownerId")
                    .append("as", "swimLanes")
            ));
    pipeline.add(context -> new Document("$project", new Document("_id", 0).append("cardTypes._id", 0)
            .append("cardTypes.cardFields", 0).append("swimLanes._id", 0)));
    return mongoOperations.aggregate(newAggregation(pipeline), "project", Document.class);
}
于 2022-01-06T16:49:53.020 回答