PHP: Push value into "array" only when it doesn't exist.

984 views
Skip to first unread message

sdotsen

unread,
Nov 26, 2009, 10:43:02 AM11/26/09
to mongodb-user
Trying to update the "tags" field but I can't get it to work
correctly. I only want to insert the value if it doesn't exist.
I'm running the latest version of the php driver.


{"_id" : ObjectId( "4b0ca6b04490c046db88a1a4") , "username" :
"james" , "tags" :
["apple","blueberry","snorkling","basketball","mini_cooper"]}

I've tried the following which seems to not have added "mini_cooper"
but if I change it to something else (i.e. "bmw"), it doesn't add.

$collection->update(array("username" => "james"), array('$push' =>
array("tags" => "mini_cooper"),true));
$collection->update(array("username" => "james"), array('$push' =>
array("tags" => "mini_cooper")), true);

dwight_10gen

unread,
Nov 26, 2009, 11:16:51 AM11/26/09
to mongodb-user
$push appends to an array, and arrays can have duplicates. so it
doesn't so what you want.

> t.findOne()
{
"_id" : ObjectId("4b0ea9947b82fa576c7bfd65"),
"username" : "james",
"tags" : [
"apple",
"blueberry",
"snorkling",
"basketball",
"mini_cooper"
]
}
> t.update({username:'james'}, { $push : { tags : 'mini_cooper' } } , true )
>
> t.find()
{ "_id" : ObjectId("4b0ea9947b82fa576c7bfd65"), "username" : "james",
"tags" : [
"apple",
"blueberry",
"snorkling",
"basketball",
"mini_cooper",
"mini_cooper"
] }

Dwight Merriman

unread,
Nov 26, 2009, 11:21:54 AM11/26/09
to mongod...@googlegroups.com
one workaround would be to first pop the value - which will do nothing if not present - and then push it, as two updates.  this should still be quite fast, and there is no client/server turnaround in between.  the only possible isseu is the operation isn't atomic - there is a short window between the updates where 'mini_cooper' would be missing from the POV of another reader (very unlikely).  if that is a concern use db.eval().



--

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.



sdotsen

unread,
Nov 26, 2009, 11:23:32 AM11/26/09
to mongodb-user
I've found the following workaround that someone else posted but I'm
having issues converting it into PHP.

update({"_id"="fruits", "types": {"$ne": "orange"}}, {"$push":
{"types": "orange"}})


On Nov 26, 11:21 am, Dwight Merriman <dwi...@10gen.com> wrote:
> one workaround would be to first pop the value - which will do nothing if
> not present - and then push it, as two updates.  this should still be quite
> fast, and there is no client/server turnaround in between.  the only
> possible isseu is the operation isn't atomic - there is a short window
> between the updates where 'mini_cooper' would be missing from the POV of
> another reader (very unlikely).  if that is a concern use db.eval().
>
> On Thu, Nov 26, 2009 at 10:43 AM, sdotsen <samnang....@gmail.com> wrote:
> > Trying to update the "tags" field but I can't get it to work
> > correctly. I only want to insert the value if it doesn't exist.
> > I'm running the latest version of the php driver.
>
> > {"_id" :  ObjectId( "4b0ca6b04490c046db88a1a4")  , "username" :
> > "james" , "tags" :
> > ["apple","blueberry","snorkling","basketball","mini_cooper"]}
>
> > I've tried the following which seems to not have added "mini_cooper"
> > but if I change it to something else (i.e. "bmw"), it doesn't add.
>
> > $collection->update(array("username" => "james"), array('$push' =>
> > array("tags" => "mini_cooper"),true));
> > $collection->update(array("username" => "james"), array('$push' =>
> > array("tags" => "mini_cooper")), true);
>
> > --
>
> > 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<mongodb-user%2Bunsu...@googlegroups.com>
> > .

Kristina Chodorow

unread,
Nov 26, 2009, 11:31:58 AM11/26/09
to mongod...@googlegroups.com
In PHP, that's:

$c->update(array("_id" => "fruits", "types" => array('$ne' =>
"orange")), array('$push' => array("types" => "orange")));

Maybe you were missing the single quotes are '$ne' or '$push'?
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.

sdotsen

unread,
Nov 26, 2009, 11:41:15 AM11/26/09
to mongodb-user
wow that was it ... i think my problem was (continue to be), is how do
I know where to place the "$" parameters.
I think I tried

"$ne => array('tags' => "orange")" and that didn't work.

I didnt realize the "$ne" had to be inside the array parameter. Like
$push, I've incremented a value by putting $inc inside the parenthesis
(i.e. array("vote_count" => array("$inc" => 1)) ).



On Nov 26, 11:31 am, Kristina Chodorow <krist...@10gen.com> wrote:
> In PHP, that's:
>
> $c->update(array("_id" => "fruits", "types" => array('$ne' =>
> "orange")), array('$push' => array("types" => "orange")));
>
> Maybe you were missing the single quotes are '$ne' or '$push'?
>

Kristina Chodorow

unread,
Nov 26, 2009, 12:02:53 PM11/26/09
to mongod...@googlegroups.com
Rule of thumb: for queries the $-operators go in the inner array and
for modifications they go in the outer array:

Query:
array("tags" => array('$ne' => "orange"))

Modification:
array('$inc' => array("count" => 1))

The first argument to update is a query, the second argument is the
modification, which is probably why it's so tricky.
Reply all
Reply to author
Forward
0 new messages