Mongodb 'like' query

249 views
Skip to first unread message

pawank

unread,
Aug 12, 2011, 8:22:24 AM8/12/11
to Lift
Dear All,

How do I make a query like below?

In mongodb, one can search like db.test.find({name: /ada/}) to find
all names having 'ada' somewhere in their name.
How do I perform this query using QueryBuilder available in lift-
mongodb?

Thanks,
Pawan

pawank

unread,
Aug 12, 2011, 8:24:07 AM8/12/11
to Lift
Also, any mechanism to get the results without QueryBuilder will do.

Tim Nelson

unread,
Aug 12, 2011, 9:30:28 AM8/12/11
to lif...@googlegroups.com
Hi Pawan,

With QueryBuilder it would be:

val qry = QueryBuilder.start("name").put("^ada".r).get

That gives you a DBObject you can feed into the find function.

If you're using 2.4-M3 (or snapshot), you can use the new BsonDSL:

import net.liftweb.mongodb.BsonDSL._

val qry = ("name" -> "^ada".r)

That gives you a JValue you can feed into the find function.

Tim


On Friday, August 12, 2011 7:24:07 AM UTC-5, pawank wrote:
Also, any mechanism to get the results without QueryBuilder will do.


pawank

unread,
Aug 12, 2011, 1:01:22 PM8/12/11
to Lift
Hello Tim,

val qry = QueryBuilder.start("name").put("^ada".r).get
is still not returning rows.
qry.toString shows {"first_name": "^pawan"} as JSON query to db
whereas db is returning results using query (mongodb docs)
db.test.find({first_name: /^pawan/});
I have tried with "/^pawan/".r regex as well but no fruitful results.
I am using lift 2.4-M2 and didn't find QueryBuilder API docs in main
lift github repository.

What could be the issue here now?

Thanks,
Pawan

y

unread,
Aug 12, 2011, 3:40:42 PM8/12/11
to Lift
Could you please provide a link to BsonDSL documentation if it exists.
I can't find it.
Thank you

Brendan W. McAdams

unread,
Aug 12, 2011, 3:48:55 PM8/12/11
to lif...@googlegroups.com
A note on performance:

MongoDB can only use indexes on Regular Expression searches that are front (^) anchored.

RegEx searches that front-anchor and then use a wildcard query like .* will use the index for as much of the un-wildcarded area as it can before scanning --- i.e., ^foo.* will run a partial scan.

Just keep that in mind for performance.  Table Scans are bad ;)

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.


Antonio Salazar Cardozo

unread,
Aug 12, 2011, 4:32:41 PM8/12/11
to lif...@googlegroups.com
The BsonDSL documentation may or may not exist, but I'll give you a briefer of what it does. Basically Lift's JsonDSL only supports the basic JSON types. BsonDSL adds support to the JsonDSL for the additional types that mongo's queries and objects support but JSON doesn't.
Thanks,
Antonio
Reply all
Reply to author
Forward
0 new messages