Search code examples
scalareactivemongo

How to use where operator in ReactiveMongo?


I am trying to use where operator in reactiveMongo to implement Full Text Search for Numeric and Text field using same API for both. But I am unable to get the correct syntax for doing it in reactive mongo.

I have followed mongoDocument "https://www.tutorialspoint.com/mongodb-regex-search-on-integer-value" to search text and Integer using where operator.But unable to implement in reactiveMongo.

collection.find(BSONDocument("$where" -> BSONDocument("$regex" -> "/^columns.*/i.test(this.columns)".r.pattern.pattern())))
     .skip(offset).cursor[M]()
     .collect[List](num,Cursor.FailOnError[List[M]]())

where M is any case class

But i am getting exception:-

DatabaseException['$where got bad type' (code = 2)]


Solution

  • right syntax for using where operator is as belows just like shell just pass the string

    collection.find(BSONDocument("$where" -> "/^r.*/i.test(this.name)"))
      .skip(offset).cursor[M]()
     .collect[List](num,Cursor.FailOnError[List[M]]())