1

I'm working with SpringBoot and Mongodb. I am new to mongodb so need a solution:

I have two collections CollA and CollB .. CollB is just the backup of CollA and contains older data.. Now I need to get all data for a time period.. How can I do that..

Example:-

**CollA** have indexes firstname lastname userAddDate  (UserAddDate is in date format)1

**CollB** also have same structure but contains older data 

So I want to get all user data who were added in certain time period let say from 2019-01-01 to 2022-01-01.. 

So 2019 data will be in CollB and 2022 will be in CollA, if it's possible to achive this in MongoDb in an optimized way .. Note CollB can contain million of records, so I don't think loading the whole collection make sense.

I am new to MongoDb have some idea about Aggregation .. union .. lookup .. but don't know if it's possible or not ..

Makarand
  • 477
  • 1
  • 7
  • 17
Subhash Rawat
  • 451
  • 6
  • 13
  • Since you need the list of new users added, you can use ColB to get the users from year 2019 and then you can use colA to get the users from 2020. This is resulting into two db queries. Will that be fine ? – Deepak Patankar May 08 '22 at 07:10
  • Yes That's possible... but I don't want to write 2 db queries for that .. is there any way I can do the union or join of 2 collection and than search .. – Subhash Rawat May 08 '22 at 07:26
  • 1
    Does this answer solves the problem https://stackoverflow.com/questions/5681851/mongodb-combine-data-from-multiple-collections-into-one-how ? – Deepak Patankar May 08 '22 at 07:31
  • yehh .. this question seems identical .. but in answer I see the other collection is dependent on first collection .. that collection contain more data about data in collection 1 so he used lookup there and did some "inner/outer join" like operation with "localField" and "foreignField".. Thanks for the link I'll see how I can make that work in my case. – Subhash Rawat May 08 '22 at 07:42
  • 1
    Also explore union with https://www.mongodb.com/docs/manual/reference/operator/aggregation/unionWith/. This is doing union instead of joins. – Deepak Patankar May 08 '22 at 07:49

1 Answers1

2

If It help someone here is the solution:-

SpringBoot solution:-

Criteria criteria = new Criteria();
        criteria.andOperator(
                Criteria.where("userAddDate")
                        .gte(fromDate),
                Criteria.where("userAddDate")
                        .lte(toDate));
        Aggregation aggregation = 
                Aggregation.newAggregation(
                        Aggregation.match(criteria),
                        UnionWithOperation.unionWith("CollB").pipeline(
                                Aggregation.match(criteria)
                        ));
        AggregationResults<Document> res =
                mongoTemplate.aggregate(
                        aggregation.withOptions(Aggregation.newAggregationOptions().allowDiskUse(true).build()),
                        "CollA",
                        Document.class);
Subhash Rawat
  • 451
  • 6
  • 13