How to multiple where using $and & $or

26 views
Skip to first unread message

dab

unread,
Apr 26, 2012, 6:43:31 AM4/26/12
to mongodb-user
Hi guys

I am trying to make a query but not working; may be Im missing
something.

Example:

MYSQL:
SELECT * from Table WHERE (id = 1 AND (name="x" OR name="y" OR
name="z"))

Mongo Example:

{"$or": [{"cat_uri": "\/bioplug\/"},{"cat_uri": "\/startsaet\/"}]}


How to put "id = 1 AND" and where in mongo query?

thanks in advanced!

Andreas Jung

unread,
Apr 26, 2012, 7:31:33 AM4/26/12
to mongod...@googlegroups.com
The OR clauses can be replaced with an $in operator.
The remaining part is trivial and left as exercise to the op.

-aj
--
ZOPYX Limited | zopyx group
Charlottenstr. 37/1 | The full-service network for Zope & Plone
D-72070 T�bingen | Produce & Publish
www.zopyx.com | www.produce-and-publish.com
------------------------------------------------------------------------
E-Publishing, Python, Zope & Plone development, Consulting


Marc

unread,
Apr 26, 2012, 11:36:26 AM4/26/12
to mongodb-user
There was another question regarding using '$and' and '$or' in the
same query asked earlier today:
http://groups.google.com/group/mongodb-user/browse_thread/thread/70aa5403e45fb7bb

Hopefully the responses there will assist you in writing your query.

The documentation on these operators, including some examples, is
provided on the "Advanced Queries" page:
http://www.mongodb.org/display/DOCS/Advanced+Queries

Please let us know if you are still having difficulty writing your
query, and we can provide further assistance.

Scott Hernandez

unread,
Apr 26, 2012, 11:44:05 AM4/26/12
to mongod...@googlegroups.com
As Andreas mentioned you can do this query without any $and/$or.

You only need $and when you repeat field names/operations which would
otherwise not be represented by a dictionary/map where the key can
only exist in one place.

The two forms are these.

find({id:1, name:{$in:["x","y","z"]}}
find({id:1, $or : [ {name:"x"}{name:"y"},{name:"z"}]}
> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> 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.
>

hitesh kumar

unread,
Apr 26, 2012, 1:03:19 PM4/26/12
to mongod...@googlegroups.com
find($and:[{id:1},$or:[{name:"x"}{name:"y"},{name:"z"}]])

I think this will work for you.. Might be.

Scott Hernandez

unread,
Apr 26, 2012, 1:05:54 PM4/26/12
to mongod...@googlegroups.com
You do not need the $and, as I mentioned on the other thread and why I
didn't include it in the example before.

While it does work, it is extra and not needed -- it is just more complicated.

dab

unread,
Apr 27, 2012, 5:46:34 AM4/27/12
to mongodb-user
Hi Scott,

This is what I was trying to do and finally i worked for me.

Thanks a lot.

Have a great weekend :)






On Apr 26, 5:44 pm, Scott Hernandez <scotthernan...@gmail.com> wrote:
> As Andreas mentioned you can do this query without any $and/$or.
>
> You only need $and when you repeat field names/operations which would
> otherwise not be represented by a dictionary/map where the key can
> only exist in one place.
>
> The two forms are these.
>
> find({id:1, name:{$in:["x","y","z"]}}
> find({id:1, $or : [ {name:"x"}{name:"y"},{name:"z"}]}
>
>
>
>
>
>
>
> On Thu, Apr 26, 2012 at 8:36 AM, Marc <m...@10gen.com> wrote:
> > There was another question regarding using '$and' and '$or' in the
> > same query asked earlier today:
> >http://groups.google.com/group/mongodb-user/browse_thread/thread/70aa...
Reply all
Reply to author
Forward
0 new messages