I'm trying to do an conditional find in MongoDB. Given a collection with the following fields:
itemId: Long
itemName: String
unconsumed: Boolean
snooze: Boolean
expiryTs: Long
I want to find all items depending on whether they are unconsumed
, depending on snooze
the expiryTs should by conditionally evaluated, like this:
if (snooze)
expiryTs < now + 1
else
expiryTs < now + 2
I use KMongo to query the collection, but with the following query
collection.find(and(PantryDomain::unconsumed eq true,
expr(cond(PantryDomain::snooze eq true,
PantryDomain::expiryTs lt Instant.now().plus(1, ChronoUnit.DAYS).toEpochMilli(),
PantryDomain::expiryTs lt Instant.now().plus(2, ChronoUnit.DAYS).toEpochMilli()))))
I get this error:
com.mongodb.MongoQueryException: Query failed with error code 16020 and error message 'Expression $lt takes exactly 2 arguments. 1 were passed in.' on server localhost:27017
Any idea what's wrong with the query? Thanks :-)
can you try
you can also simplify it as