0

I have two collections called 'store' and 'items' , where store keep all product records belongs to the supermarket with timestamp. And 'items' keeps records of some details about purchases.

Store Collection

{{"id": "802736816700002577966429","$date": "2021-05-05T07:02:51Z","state":"VALID"},{"id": "802736816700002577966430","$date": "2021-07-27T07:02:51Z","state":"NOT VALID"},{"id": "802736816700002577966430","$date": "2021-04-27T07:02:51Z","state":"VALID"}}

Item Collection

{{"id": "802736816700002577966429","$date": "2021-05-05T07:02:51Z"},{"id": "802736816700002577966430","$date": "2021-07-27T07:02:51Z"},{"id": "802736816700002577966430","$date": "2021-07-27T07:02:51Z"}}

The query which i want to execute is ->

Select items from Store Collection where state != VALID and timestamp<2021-05-10 and then delete corresponding items from Item Collection

Please provide Spring-boot criteria/aggregation code which is similar to

Criteria criteriaEvt = new Criteria(....);

Aggregation aggregationEvt = newAggregation(.....)

2
  • You need to do it in 2 queries. In the first query you find the "corresponding items" ($lookup with sub-query) and then remove them db.item.remove({_id:{$in:[id1, id2, ..., idn]}}) Commented Aug 11, 2021 at 11:59
  • 1
    Run this query https://mongoplayground.net/p/R2poC_iSypY and remove 1 by 1 o remove with $in expression stackoverflow.com/a/58730480/3710490 Commented Aug 11, 2021 at 12:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.