【问题标题】:SpringData MongoDB Aggregation Projection excludeSpringData MongoDB聚合投影排除
【发布时间】:2021-08-05 06:50:52
【问题描述】:

我正在尝试使用项目排除(必要的)字段。我在这个问题上挣扎了很长时间。我可以排除其他(规范之外)字段,但无法排除规范字段中包含必要的字段。

当我使用 spec_id 请求时,我期望:

[
    {
        "spec": {
            "spec_id": "5",
            "parent_id": "2",
            "material": "plastic",
            "size": "L"
        }
    }
]

但它会返回

[
    {
        "spec": {
            "spec_id": "5",
            "parent_id": "2",
            "necessary": {
                "unit": "20X20",
                "dateOfManufacture": "2020",
                "madeIn": "KNOWN",
                "producer": "KNOWN",
                "importer": "KNOWN",
                "productionLocation": "KNOWN"
            },
            "material": "plastic",
            "size": "L"
        }
    }
]

我这里有方法

public List<UnwindInventory> selectSpecWithoutNecessary(String spec_id) {
        UnwindOperation unwindOperation = Aggregation.unwind("spec");
        ProjectionOperation project = Aggregation.project().andExclude("name", "spec.necessary");
        MatchOperation match = Aggregation.
                match(Criteria.where("spec._id").is(spec_id));
        Aggregation aggregation = Aggregation.newAggregation(unwindOperation, match, project);

        return mongoOperations.aggregate(aggregation, Inventory.class, UnwindInventory.class).getMappedResults();

这是dto

@NoArgsConstructor
@AllArgsConstructor
@Data
public class UnwindInventory {
    private String inventory_id;
    private String name;
    private Spec spec;
    private Necessary necessary;

}

【问题讨论】:

  • 你能解释清楚点吗!
  • 我编辑我的问题!

标签: mongodb spring-data


【解决方案1】:

解决了。我手动投影它们中的每一个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多