Re: [mongodb-user] Need MongoDB query in PHP

198 views
Skip to first unread message

Sam Millman

unread,
Nov 2, 2012, 8:06:01 AM11/2/12
to mongod...@googlegroups.com
The default operator between array elements in MongoDB is $and, as such you can actually remove those $and's and use only the $or. Also you cannot have duplicate array keys within a single array as such only one $and would be seen anyway.


On 2 November 2012 10:47, Jay Prakash Mishra <jaypra...@gmail.com> wrote:
Dear Friend,

I need a customize query criteria for find method. My condition of query is like below :

Mysql Query :

Select * from students where (state='xx' and city='yy') OR (university='mm' and college='nn').

Please provide above query in mongodb. However I tried to write this in Mongo as below :

array(
       '$or'=>array(
               '$and'=>array(
                       array('state' => array('$in' => array('Delhi','Bihar'))),
                       array('city' => array('$in' => array('Delhi','Bihar'))),
               ),
               '$and'=>array(
                               array('university' => array('$in' => array('New Delhi','Patna'))),
                               array('college' => array('$in' => array('New Delhi','Patna')))
               )
       )
)


But it produces fatal errors :

Fatal error: Uncaught exception 'MongoCursorException' with message '$and expression must be a nonempty array'  ...............................


Please help me.

Regards
Jay

--
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

Jay Prakash Mishra

unread,
Nov 2, 2012, 8:17:04 AM11/2/12
to mongod...@googlegroups.com
Hi Sam,

I have written the like below after you suggestion :

array(
'$or'=>array(
array(
array('pState' => array('$in' => array('Delhi','Bihar'))),
array('pCity' => array('$in' => array('New Delhi','Patna'))),
),
array(
array('boardPercent_10' => array('$gt' => 50)),
array('boardName_10' => array('$in' => array('cbse','icse')))
)
)
)

But it is still producing error?
--
Thanks & Regards
Jay Prakash | Software Engineer
M. N. - 9953477708

Sam Millman

unread,
Nov 2, 2012, 8:42:55 AM11/2/12
to mongod...@googlegroups.com
You've got a few too many arrays there, the syntax for an $or is $or => array(array(//condition), array(//condition)) with the condition parts being $and and the sub arrays in the $or array being $or
Reply all
Reply to author
Forward
0 new messages