How do we query on an embedded BsonRecord field

46 views
Skip to first unread message

Rohit Rai

unread,
Jun 11, 2011, 3:49:52 AM6/11/11
to lif...@googlegroups.com
This is a question regarding this User class -

I have profile BsonRecord embedded in User, now during search I want to search the User based on his/her displayName that is part of the the profile.

I have 2 queries in this -

User.findAll("profile.displayName" -> filter)  or User.findAll("profile.is.displayName" -> filter) ?

According to my understanding this will give only an exact match... Hpw can I do an "like" query so that I can give results i the user submits half a name too.

Regards,
Rohit

Rohit Rai

unread,
Jun 11, 2011, 5:31:13 AM6/11/11
to lif...@googlegroups.com
Just to confirm, I tried it and this works with exact value match - User.findAll("profile.displayName" -> filter) 

but am still trying to figure out the query to get a match from a partial name (sql like).

Tim Nelson

unread,
Jun 11, 2011, 7:58:05 AM6/11/11
to lif...@googlegroups.com

Rohit Rai

unread,
Jun 11, 2011, 4:22:43 PM6/11/11
to lif...@googlegroups.com

Hi Tim,
I saw that and can use regex in mongodb... But I am not sure how to build a regex query in json dsl or query builder in lift.

Are there any examples of it?

Regards,
Rohit

> --
> You received this message because you are subscribed to the Google Groups "Lift" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/liftweb/-/GnYArm35UlIJ.
> 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.
>

Tim Nelson

unread,
Jun 12, 2011, 7:26:48 AM6/12/11
to lif...@googlegroups.com
Hi,

To build a regex query using the json dsl, you need to use the PatternSerializer [1]. Add that to your record's formats and your query would look like:

User.findAll("profile.displayName" -> (("$regex" -> "/regex") ~ ("$flags" -> 0)))

If you want to use the java-mongo-driver directly, I believe you need to use BasicDBObjectBuilder.

You may also be able to use Rogue [2]. See line 121.

Tim

Brendan W. McAdams

unread,
Jun 12, 2011, 8:31:32 AM6/12/11
to lif...@googlegroups.com

Keep in mind as well that Rebecca queries in MongoDB can only hit an index if you anchor to the start of the field (^). The query profiler output can sometimes be misleading on this point but we are unable to use indexes without a start anchor.

If you want to avoid tablescans there are a few tips for approaches on the MongoDB wiki ( http://www.mongodb.org/display/DOCS/Full+Text+Search+in+Mongo).  Native full text search is on the roadmap for sometime this year as well.

On Jun 12, 2011 7:27 AM, "Tim Nelson" <tnel...@gmail.com> wrote:

Brendan W. McAdams

unread,
Jun 12, 2011, 9:27:11 AM6/12/11
to lif...@googlegroups.com
That should have read 'regex' queries. Damn autocorrect.

Antonio Salazar Cardozo

unread,
Jun 12, 2011, 11:31:39 AM6/12/11
to lif...@googlegroups.com
My personal approach is to do something like:

val regex = """^/hello""".r
User.findAll("profile.displayName" -> decompose(regex))

The downside is it requires that you declare an implicit val (implicit val formats = User.formats is what I do), but I find it reads more naturally. You also have to import net.liftweb.json.Extraction._ to get the decompose function.
Thanks,
Antonio

Rohit Rai

unread,
Jun 14, 2011, 1:39:26 PM6/14/11
to lif...@googlegroups.com
Thanks for the replies guys... I have not yet got a chance to try this out... will reply as soon as I am able to work on this part again.

Regards,
Rohit

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To view this discussion on the web visit https://groups.google.com/d/msg/liftweb/-/FyD5DKOEy6MJ.
Reply all
Reply to author
Forward
0 new messages