1

我正在尝试在 MongoDB 中进行条件查找。给定一个包含以下字段的集合:

itemId: Long
itemName: String
unconsumed: Boolean
snooze: Boolean
expiryTs: Long

我想根据是否为 ​​来查找所有项目unconsumed,具体取决于snooze应有条件地评估的 expiryT,如下所示:

if (snooze)
    expiryTs < now + 1
else
    expiryTs < now + 2

我使用 KMongo 来查询集合,但使用以下查询

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()))))

我收到此错误:

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

知道查询有什么问题吗?谢谢 :-)

4

1 回答 1

0

你能试一下吗

collection.find(and(PantryDomain::unconsumed eq true,
                    expr(cond(PantryDomain::snooze eq true,
                              lt PantryDomain::expiryTs Instant.now().plus(1, ChronoUnit.DAYS).toEpochMilli(),
                              lt PantryDomain::expiryTs Instant.now().plus(2, ChronoUnit.DAYS).toEpochMilli()))))

您也可以将其简化为

collection.find(and(PantryDomain::unconsumed,
                    expr(cond(PantryDomain::snooze,
                              lt PantryDomain::expiryTs Instant.now().plus(1, ChronoUnit.DAYS).toEpochMilli(),
                              lt PantryDomain::expiryTs Instant.now().plus(2, ChronoUnit.DAYS).toEpochMilli()))))
于 2019-01-20T20:43:18.170 回答