简体   繁体   中英

$and in reactive mongo with play framework 2.6

I' am using play framework v2.6 with scala, I'am using reactivemongo as driver for mongodb, my problem is I do not know how to retrieve objects having values that are between two values.In my example i have this case class :

case class Card (id : String, creationDate: Date)

I want to retrieve cards that have a creation date between two dates so i used this query:

val query = BSONDocument(
         "$and" -> BSONDocument(
         "creationDate" -> Json.obj("$gte" ->startDate),
         "creationDate" -> Json.obj("$lte" ->endDate))
         )

but this outputs the following error:

A server error occurred DatabaseException['Can't canonicalize query:       
BadValue and needs an array' (code = 17287)]

According to mongo documentation the $and operator needs an array:

val query = BSONDocument(
  "$and" -> BSONArray(List(
    BSONDocument("creationDate" -> Json.obj("$gte" ->startDate)),
    BSONDocument("creationDate" -> Json.obj("$lte" ->endDate))
  ))
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM