I have a situation that I want to provide 3 conditions in elemMatch method call provided by org.springframework.data.mongodb.core.query.Criteria Api.
Inventory collection looks like this -
{
"_id" : "H-P3NCDST45"
"booking" : [
{
"updateDate" : ISODate("2018-01-29T13:32:03.789Z"),
"status" : "STARTED",
"message" : "abc"
},
{
"updateDate" : ISODate("2018-01-29T13:32:04.789Z"),
"status" : "PENDING",
"message" : "def"
},
{
"updateDate" : ISODate("2018-01-29T13:33:04.789Z"),
"status" : "CONFIRMED",
"message" : "abc"
}
]
}
I want to do something like below query. Fetch all documents from inventory collection which have at-least one embedded document in booking array whose status is "STARTED" and updateDate is between <start> and <end> date, i.e. Fetch all booking from inventory which are started/initiated between given <start> and <end> date.
db.inventory.find({"booking": { $elemMatch: { status: "STARTED",updateDate: { $gte: ISODate("2018-01-01T14:04:34.447Z"), $lte: ISODate("2018-01-20T14:04:34.447Z")}}}})
Criteria Api provides criteria.elemMatch(Criteria c). How to pass multiple criteria in this elemMatch(c) method call?
You can try
Criteria criteria =
Criteria.where("booking").
elemMatch(
Criteria.where("status").is("STARTED").
and("updateDate").gte(date1).lte(date2)
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With