Query for search, sort and paging

83 views
Skip to first unread message

Chris Nicola

unread,
Jul 14, 2011, 12:27:32 AM7/14/11
to mongod...@googlegroups.com
I'm trying to do a query for a collection that allows me to page, sort, filter and apply a keyword search all at the same time.  The problem is mongo doesn't seem to allow for this since the keyword search requires an OR'd regex on multiple fields.  I get an error saying AND only works with equality comparisons.  Is there another way I can do this.  I would like to avoid creating a field just for the keyword searching itself.

Thanks

Alvin Richards

unread,
Jul 14, 2011, 4:17:43 AM7/14/11
to mongodb-user
You can combine skip and limit to give you the required functionality,
see

http://www.mongodb.org/display/DOCS/Advanced+Queries

Can you paste an sample document and your query that was causing a
problem?

-Alvin

Chris Nicola

unread,
Jul 14, 2011, 11:41:20 AM7/14/11
to mongod...@googlegroups.com
Yes I've got the page and sort part down, it's the query itself.  And of course if I can't filter on the server that makes paging and sorting a bit of a pain because I'll have to return all results and filter them in code which is far less than idea.

The query (my query language is a little rusty, I will update this with the exact query later) was something like:

{ $in { Categories : [<list-of-categories>] }, $in { CatalogId: [<list-of-catalog-ids>] }, $or : { Model: /<some keyword>/i, Manufacturer: /<some keyword>/i, Description: /<some keyword>/i, Identifiers: <some keyword> }}

Basically search for something that:
- Is in any one of a list of categories (note Categories is an array also I'm assuming $in still works)
AND
- Is in one of a list of catalog ids
AND
- Contains some keyword in either
  - Model
  OR
  - Manufactuer
  OR 
  - Description
  OR
  - Has any identifier value matching some keyword

Anyways, it says it can't AND these queries together.  I've also tried adding one EQ query for each of the categories I'm searching for instead of using $in, but that doesn't seem to be the issue.

Eliot Horowitz

unread,
Jul 15, 2011, 3:30:25 AM7/15/11
to mongod...@googlegroups.com
If you can send the exact query with an error message that would be helpful.

> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mongodb-user/-/vPU-L6Z0bwoJ.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/mongodb-user?hl=en.
>

Reply all
Reply to author
Forward
0 new messages