Advenced query mongo

14 views
Skip to first unread message

Giovanni Fabio

unread,
May 19, 2012, 1:37:27 PM5/19/12
to mongod...@googlegroups.com
Mysql = SELECT * FROM table_name WHERE (a=1 OR b=1) AND (c=1 or d=1)
MongoDb = ???

Please help me!

Scott Hernandez

unread,
May 19, 2012, 5:37:40 PM5/19/12
to mongod...@googlegroups.com
See $and/$or --
http://www.mongodb.org/display/DOCS/Advanced%20Queries#AdvancedQueries-%24or

find({$and:[{$or:[{a:1}, {b:1}]}, {$or:[{c:1}, {d:1}]} ]})
> --
> 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
> See also the IRC channel -- freenode.net#mongodb

Giovanni Fabio

unread,
May 19, 2012, 7:19:32 PM5/19/12
to mongod...@googlegroups.com
Ok solved
mysql
SELECT * FROM table_name WHERE (a=1 OR b=1) AND (a=2 or b=2) 

PERL

use MongoDB;
use MongoDB::OID;
my $conn = MongoDB::Connection->new;
my $db = $conn->database;
my $users = $db->database;

my $all_users = $users->find(
{
'$and' => [{ "a" => {'$in' => ["1" ,"2"]},
"b" => {'$in' => ["1" ,"2"]}}],
}
);

mongodb query =
db.database.find({ $and: [{"a" : { $in : [1,2]}, {"b" : { $in : [1,2]} }) 

Scott Hernandez

unread,
May 19, 2012, 7:57:58 PM5/19/12
to mongod...@googlegroups.com

You do not need the $and.

--
Reply all
Reply to author
Forward
0 new messages