How to use where operator in reactive mongo.

137 views
Skip to first unread message

Raj Bala

unread,
Oct 4, 2019, 8:40:15 AM10/4/19
to ReactiveMongo - http://reactivemongo.org
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.

 code:-   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)]

Cédric Chantepie

unread,
Oct 4, 2019, 10:05:06 AM10/4/19
to ReactiveMongo - http://reactivemongo.org
Have you first try the query in MongoShell. It's always recommended to design working queries.

The error is a server one, not specific to the driver.

Raj Bala

unread,
Oct 5, 2019, 1:01:37 AM10/5/19
to ReactiveMongo - http://reactivemongo.org


On Friday, 4 October 2019 19:35:06 UTC+5:30, Cédric Chantepie wrote:
Have you first try the query in MongoShell. It's always recommended to design working queries.

The error is a server one, not specific to the driver.


Yes, I have tried it in mongo shell, It's working over there. 

Cédric Chantepie

unread,
Oct 5, 2019, 7:08:38 AM10/5/19
to ReactiveMongo - http://reactivemongo.org
If the very same query is working in shell, in will work with the driver. Check the parameters that can differ from your shell test.

Md. Mahadi Hossain

unread,
Oct 5, 2019, 8:07:26 AM10/5/19
to reacti...@googlegroups.com
have you tested without removing  ".r.pattern.pattern()"  or changing your pattern?


--
You received this message because you are subscribed to the Google Groups "ReactiveMongo - http://reactivemongo.org" group.
To unsubscribe from this group and stop receiving emails from it, send an email to reactivemong...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/reactivemongo/c6a65e40-405f-41f0-9d39-7b52a2e7f630%40googlegroups.com.

Raj Bala

unread,
Oct 5, 2019, 8:52:09 AM10/5/19
to ReactiveMongo - http://reactivemongo.org
  1. db.DB333.find({ $where: "/^2.*/i.test(this.age)" })

i.e. what i have tried in mongo shell.Here, 'age' is a parameter to search. and will get all the ages whose starting value is '2'.

Raj Bala

unread,
Oct 5, 2019, 9:00:40 AM10/5/19
to ReactiveMongo - http://reactivemongo.org

  1. db.DB333.find({ $where: "/^2.*/i.test(this.age)" })

i.e. what i have tried in mongo shell.Here, 'age' is a parameter to search. and will get all the ages whose starting value is '2'.

No, I didn't tried with .r.pattern().pattern(). Because in mongo shell, regex works with double quotes.But in reactive mongo, to make it understand a regex pattern it is, and not a string , I used ".r". And because BSONDocument requires BSONElement, I have used ".pattern().pattern()".

On Saturday, 5 October 2019 17:37:26 UTC+5:30, Md. Mahadi Hossain wrote:
have you tested without removing  ".r.pattern.pattern()"  or changing your pattern?


On Fri, Oct 4, 2019 at 6:40 PM Raj Bala <r...@ficode.in> wrote:
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.

 code:-   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)]

--
You received this message because you are subscribed to the Google Groups "ReactiveMongo - http://reactivemongo.org" group.
To unsubscribe from this group and stop receiving emails from it, send an email to reacti...@googlegroups.com.

osxhacker

unread,
Mar 7, 2021, 11:01:53 PM3/7/21
to ReactiveMongo - http://reactivemongo.org
An easy way to specify a regex is to use the String Operators provided by the ReactiveMongo-Criteria DSL.

Full disclosure: I am the author of that library.

HTH
Reply all
Reply to author
Forward
0 new messages