How to use $month as part of logical operators in MongoDB in Java?

250 views Asked by At

I have a document {_id: abc, orderdate:(ISO date), quantity:10} and I am trying to come up with query in Java where we search for all documents with orders in a given month whose quantity is > 5 Eg: select * from documents where month(orderdate)=Jan and quantity>5

Can you please help me write this query in Java?

1

There are 1 answers

0
R C On

After going thru Search by month in MongoDB

I learnt about $expr from db.customer.find({ "$expr": { "$eq": [{ "$month": "$bday" }, 9] } }) and converted to

db.getCollection('customer').find({
"$and":[{"$expr": { "$eq": [{ "$month": "$bday" }, 9] }},{"firstName":"John"}]
})

and then get java code using

Document.parser("db.getCollection('custoer').find({"$and":[{"$expr": { "$eq": [{ "$month": "$bday" }, 9] }},{"firstName":"John"}]})")

at the end java code looks like

Filters.and(Arrays.asList(Filters.expr(new Document().append("$in", Arrays.asList(new Document().append("$month", "$bday"),9))),Filters.eq("firstName","John")))