简体   繁体   English

Spring数据mongodb聚合按列表过滤

[英]Spring data mongodb aggregation filter by list

I followed the example in this question Create filter aggregation in spring to create filter in aggregation on spring data mongodb.我按照这个问题中的示例在spring中创建过滤器聚合以在 spring 数据 mongodb 上创建聚合中的过滤器。 In the example the filter search by valueOf and equalToValue to create an $eq.在示例中,过滤器通过valueOfequalToValue搜索以创建 $eq。 How can I create an $in and pass a list?如何创建 $in 并传递列表?

This is my code:这是我的代码:

ProjectionOperation p2 = project()
            .andExpression("name").as("name")
            .andExpression("version._id").as("version._id")
            .andExpression("version.version").as("version.version")
            .and(filter("version.cat")
                    .as("cat")
                    .by(valueOf("cat._id")
                            .equalToValue(new ObjectId("5de99f42f15273c92d1228e8"))))
            .as("version.cat");

and I'm looking for something like this:我正在寻找这样的东西:

**ProjectionOperation p2 = project()
            .andExpression("name").as("name")
            .andExpression("version._id").as("version._id")
            .andExpression("version.version").as("version.version")
            .and(filter("version.cat")
                    .as("cat")
                    .by(new Criteria("cat")
                            .in(Arrays.asList(
                                    new ObjectId("5de99f42f15273c92d1228f7"),
                                    new ObjectId("5de99f42f15273c92d1228f9"))
                            )
                            .getCriteriaObject()
                    )).as("version.cat"));

Thank you for your answers.谢谢您的回答。

I can resolve this problem with ArrayOperators :我可以用ArrayOperators解决这个问题:

ProjectionOperation p2 = project()
            .andExpression("name").as("name")
            .andExpression("version._id").as("version._id")
            .andExpression("version.version").as("version.version")
            .and(filter("version.cat")
                    .as("cat")
                    .by(
                            ArrayOperators.In
                                    .arrayOf(Arrays.asList(
                                            new ObjectId("5de99f42f15273c92d1251853"),
                                            new ObjectId("5de99f42f15273c92d1251854"),
                                    )
                                    ).containsValue("$$cat._id")))
            .as("version.cat");

In arrayOf I pass the list that I want to compare with the value of each objectId.在 arrayOf 中,我传递了要与每个 objectId 的值进行比较的列表。 I think, the filter makes some class of 'for' to get each value of the cat list.我认为,过滤器制作了一些“for”类来获取 cat 列表的每个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM