Mongo 聚合基于多个集合之间的日期

发布于 2025-01-11 03:23:17 字数 1693 浏览 0 评论 0原文

我使用 Mongo Aggregation 和 Spring Data 从 4 个集合中检索数据。 我的问题是我想添加一个基于其中 3 个集合的 createdAt 的过滤器。 这里是例子:

动物

{"_id":123,"name":"Lucky","age":3,createdAt:"2022-02-01 10:00:00.000Z"}
{"_id":456,"name":"Joe","age":5,createdAt:"2022-02-10 20:03:00.000Z"}

{"idAnimal":123,"toy":6,createdAt:"2022-03-01 10:00:40.000Z"}
{"idAnimal":456,"toy":2,createdAt:"2022-02-10 20:05:00.000Z"}

宠物

{"idAnimal":123,"meal":3,"medicine":false,createdAt:"2022-03-01 10:00:40.000Z"}
{"idAnimal":456,"meal":4,"medicine":true,createdAt:"2022-02-10 20:05:00.000Z"}

我的意思是让所有带有 Pet 集合的动物创建 gte(2022-03- 01)。预期的结果将是第一只猫,Lucky。 在我的代码中,我尝试了这个

final List<Criteria> criteria = new ArrayList<>();
criteria.add(new Criteria().orOperator(
                    Criteria.where("createdAt").gte("2022-03-01").lte("2022-03-02"),
                    Criteria.where("Cat.createdAt").gte("2022-03-01").lte("2022-03-02"),
                    Criteria.where("Pet.createdAt").gte("2022-03-01").lte("2022-03-02")
            ));

,我的聚合设置是:

Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.lookup("cat", "_id", "idAnimal", "Cat"),
                Aggregation.lookup("pet", "_id", "idAnimal", "Pet"),
                Aggregation.unwind("$_id"),
                Aggregation.match(new Criteria().andOperator(criteria.toArray(new Criteria[0]))
                )
        );

我尝试交换条件,首先查询宠物,但没有按预期进行。当我运行这个时,我没有得到任何结果。 你有什么建议吗? 是否可以对多个集合上的日期执行聚合? 先感谢您!

I'm using Mongo Aggregation with Spring Data to retrieve data from 4 collections.
My problem is that I want to add a filter based on the createdAt on 3 of these collections.
Here the examples:

Animal

{"_id":123,"name":"Lucky","age":3,createdAt:"2022-02-01 10:00:00.000Z"}
{"_id":456,"name":"Joe","age":5,createdAt:"2022-02-10 20:03:00.000Z"}

Cat

{"idAnimal":123,"toy":6,createdAt:"2022-03-01 10:00:40.000Z"}
{"idAnimal":456,"toy":2,createdAt:"2022-02-10 20:05:00.000Z"}

Pet

{"idAnimal":123,"meal":3,"medicine":false,createdAt:"2022-03-01 10:00:40.000Z"}
{"idAnimal":456,"meal":4,"medicine":true,createdAt:"2022-02-10 20:05:00.000Z"}

What I mean to do is to get all the Animals with a Pet collection created gte(2022-03-01). The expected result would be the first cat, Lucky.
In my code I tried this

final List<Criteria> criteria = new ArrayList<>();
criteria.add(new Criteria().orOperator(
                    Criteria.where("createdAt").gte("2022-03-01").lte("2022-03-02"),
                    Criteria.where("Cat.createdAt").gte("2022-03-01").lte("2022-03-02"),
                    Criteria.where("Pet.createdAt").gte("2022-03-01").lte("2022-03-02")
            ));

and my Aggregation set up is:

Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.lookup("cat", "_id", "idAnimal", "Cat"),
                Aggregation.lookup("pet", "_id", "idAnimal", "Pet"),
                Aggregation.unwind("$_id"),
                Aggregation.match(new Criteria().andOperator(criteria.toArray(new Criteria[0]))
                )
        );

I tried to swap the criteria, querying the Pet first, but didn't go as expected. When I run this, I get no result.
Do you have any tips? Is it possibile to execute an aggregation on Dates over multiple collections?
Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文