problem with $in statement in PHP

404 views
Skip to first unread message

Artee

unread,
Sep 6, 2010, 4:36:09 PM9/6/10
to mongodb-user
How should I use PHP to make dynamic array like this (for example
using data from other collection)?

$sth=array(1,2,3,4,5);

I must use array like $sth in my mongo query:

$collection->find(array('field')=>array("$in"=>$sth))

Always when I create array, I obtain:

$sth[0]=1;
$sth[1]=2;
$sth[2]=3;
$sth[3]=4;
$sth[4]=5;

Of course this array cannot works when I insert it to my query.
In Python I have append function, but in PHP...?

Please help me :)

Gabriel Sosa

unread,
Sep 6, 2010, 7:01:48 PM9/6/10
to mongod...@googlegroups.com
dont use the $ inside quotes ("). Use (') otherwise php will try to
replace the content of $in (in this case empty) more info in [1]

your sentence should be

$collection->find(array('field')=>array('$in'=>$sth))

or otherwise

$collection->find(array('field')=>array("\$in"=>$sth))


Saludos

[1] http://bit.ly/9q3GQx

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

--
Gabriel Sosa
Si buscas resultados distintos, no hagas siempre lo mismo. - Einstein

Steve Kamerman

unread,
Sep 6, 2010, 7:20:27 PM9/6/10
to mongod...@googlegroups.com
You can change the modifier from "$" to something else, like ":" in PHP to prevent this type of issue.  http://us3.php.net/manual/en/mongo.tutorial.php (see "mongo.cmd").  The change can be made in php.ini if you want it global, or just put this at the top of your script:

ini_set("mongo.cmd", ":");
Reply all
Reply to author
Forward
0 new messages