an example of how to use $or with PHP driver

2,187 views
Skip to first unread message

sa9

unread,
Aug 10, 2010, 10:11:54 AM8/10/10
to mongodb-user

I couldn't implement $or query modifier using the following code:

$query = array( "sample_id" => $sample_id , '$or' => array("ID" =>
"." , "in_seeds"=> 1));
$cursor = $collection->find( $query );

I didn't find an example of how to use $or in PHP mongodb driver.

Can any be kind enough to correct the code or to provide and example?


Thank you very much.
Sal

iwind

unread,
Aug 10, 2010, 10:34:35 AM8/10/10
to mongodb-user
SQL: where ID="." or in_seeds=1

=>

array(
'$or' => array(
array("ID" => "."),
array("in_seeds" => 1)
)
)


You can see that, every sub-array is a condition.

sa9

unread,
Aug 10, 2010, 6:10:34 PM8/10/10
to mongodb-user
Thank you iwind for your prompt answer.

I tried your suggestion but it didn't work, here is the returned
message:

Fatal error: Uncaught exception 'MongoCursorException' with message
'invalid operator: $or' in /PHP_driver/test.php:72 Stack trace: #0 /
PHP_driver/test.php(72): MongoCursor->rewind() #1 {main} thrown in/
PHP_driver/test.php on line 72

line 72 is : foreach ($cursor as $obj) { .... etc

Sal.

Eliot Horowitz

unread,
Aug 10, 2010, 6:30:41 PM8/10/10
to mongod...@googlegroups.com
What version are running?
You need to be using 1.6.0 for $or

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

sa9

unread,
Aug 11, 2010, 2:47:37 PM8/11/10
to mongodb-user
I was using 1.4.4 and then I installed 1.6.0 version but I still get
the same error:

This is my final query:

$query = array( "sample_id" => $sample_id , array(
'$or' => array(
array("ID" => "."),
array("in_seeds" => 1)
)
)
);

Sal.

On Aug 10, 11:30 pm, Eliot Horowitz <eliothorow...@gmail.com> wrote:
> What version are running?
> You need to be using 1.6.0 for $or
>

Michael Dirolf

unread,
Aug 11, 2010, 2:49:18 PM8/11/10
to mongod...@googlegroups.com
Are you sure you restarted after upgrading? Can you try db.version()
from the shell?

sa9

unread,
Aug 11, 2010, 2:58:28 PM8/11/10
to mongodb-user
Yes, it is 1.6.0


On Aug 11, 7:49 pm, Michael Dirolf <m...@10gen.com> wrote:
> Are you sure you restarted after upgrading? Can you try db.version()
> from the shell?
>

Michael Dirolf

unread,
Aug 11, 2010, 3:03:11 PM8/11/10
to mongod...@googlegroups.com
Does the same query work from the shell?

Mitch Pirtle

unread,
Aug 11, 2010, 4:28:44 PM8/11/10
to mongod...@googlegroups.com
Don't forget to upgrade your mongodb extension for php - that one came
out just last week IIRC.

-- Mitch

sa9

unread,
Aug 11, 2010, 7:49:07 PM8/11/10
to mongodb-user
Thanks guys,

@Michael Dirolf
Yes. Here is the query at the shell:

> db.snvs.find({'sample_id':'4c62fd1fa9a4e8165c000000',$or:[{'ID':'.'},{'in_seeds':1}]}).count()
3105

@Mitch Pirtle
I am using MongoDB at Bluehost hosting sever (I got my own IP address
to run Mongodb). Because they don't permit 'sudo' command via ssh ,
the help desk people have installed the PECL for mongodb PHP driver
not 2 days ago. So, most likely , it is the latest version but in any
case: how can I check for the version?

Mitch Pirtle

unread,
Aug 11, 2010, 10:44:10 PM8/11/10
to mongod...@googlegroups.com
On Wed, Aug 11, 2010 at 7:49 PM, sa9 <salt...@gmail.com> wrote:
>
> @Mitch Pirtle
> I am using MongoDB at Bluehost hosting sever (I got my own IP address
> to run Mongodb). Because they don't permit 'sudo' command via ssh ,
> the help desk people have installed the PECL for mongodb PHP driver
> not 2 days ago. So, most likely , it is the latest version but in any
> case: how can I check for the version?

phpinfo() should tell you what version in the Mongo section. Current is 1.0.9.

-- Mitch

sa9

unread,
Aug 12, 2010, 4:27:42 AM8/12/10
to mongodb-user
Well, yes, it is 1.0.9.

On Aug 12, 3:44 am, Mitch Pirtle <mitch.pir...@gmail.com> wrote:

sa9

unread,
Aug 12, 2010, 11:14:40 AM8/12/10
to mongodb-user
Ok , I finally got it to work!

I just removed the 'array' before '$or' . So the correct code is:
--------
$query = array( "sample_id" => $sample_id ,
'$or' => array(
array("ID" => "."),
array("in_seeds" => 1)
)
);
----------
instead of:

$query = array( "sample_id" => $sample_id , array(
'$or' => array(

array("ID" => "."),

array("in_seeds" => 1)
)
)
);
-------

Thanks everyone, I really appreciate your help.

Sal.

sa9

unread,
Aug 12, 2010, 11:14:48 AM8/12/10
to mongodb-user
Ok , I finally got it to work!

I just removed the 'array' before '$or' . So the correct code is:
--------
$query = array( "sample_id" => $sample_id ,
'$or' => array(
array("ID" => "."),
array("in_seeds" => 1)
)
);
----------
instead of:

$query = array( "sample_id" => $sample_id , array(
'$or' => array(

array("ID" => "."),

array("in_seeds" => 1)
)
)
);
-------

Thanks everyone, I really appreciate your help.

Sal.


On Aug 12, 9:27 am, sa9 <saltu...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages