Reputation: 922
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 ..
Upvotes: 1
Views: 1487
Reputation: 922
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);
Upvotes: 3