Link to home
Start Free TrialLog in
Avatar of Mike Potter
Mike Potter

asked on

php and MongoDb Update or delete array item

I am using php and mongoDb. I am new to mongoDb, but fine with php.  In my mongoDb database I have a  collection called 'users' with a number of fields such as firstname, lastname etc. There are a number of users.


I also have a field/collection in some users called web_links which contains an array.
ie: within my users document:
{
    "_id" : ObjectId("587af11ec09cf31a1955ed92"),
    "username" : "mike",
    "firstname" : "Mike",
    "lastname" : "Tester",
    "email" : "mike@test.com",
    "web_links" : [
        {
            "name" : "google",
            "link" : "https://google.com",
            "status" : "1",
            "added" : ISODate("1970-01-18T08:00:57.600Z")
        },
        {
            "name" : "yahoo",
            "link" : "https://yahoo.com",
            "status" : "1",
            "added" : ISODate("1970-01-18T08:00:57.600Z")
        }
    ]
}

I am trying to firstly update the status of an array item.
I have got as far as this ->

$name = 'google';
$newstatus = '0';

$this->database->users->updateOne(['_id'=>new MongoDB\BSON\ObjectId($userid)],['web_links.name'=>$name],['$set'=>['web_links.$.status'=>$newstatus]]);

This is returning the following error via firebug:
Uncaught exception 'MongoDB\Exception\InvalidArgumentException' with message 'First
 key in $update argument is not an update operator'.

I have tried to change $set to $push, but that is not the answer.

I would be very grateful for assistance to learn to update an array item and also delete an array, for example,  from web_links where name='google'.

Thanks for any help and feedback
Mike
Avatar of HainKurt
HainKurt
Flag of Canada image

Avatar of Mike Potter
Mike Potter

ASKER

Thanks so much for your response, giving a go now.
I have sorted out the delete. Ill post incase it helps anyone :)
$this->database->users->updateOne(['_id'=>new MongoDB\BSON\ObjectId($userid)],['$pull'=>['web_links'=>['name'=>$name]]]);
We're moving forward :)
different error to sort on the array update (the delete above is fine):
Uncaught exception 'MongoDB\Driver\Exception\InvalidArgumentException' with message
 'Replacement document may not contain "$" or "." in keys
I cant see any superfluous apostrophes, but thinking hard!

Thanks again
what happens if you remove all ' from code above
or use " instead
will have a try , thanks Huseyin
unfortunately no good on that one. Without quotes wouldn't run. Single to double quotes no different . Thanks
I have tried variations in bracketing. Here is where Im currently at. Appreciate any mongoDb/php suggestions. Just re-iterating that web_links is an array object within users. Thanks

$this->database->users->replaceOne(['_id'=>new MongoDB\BSON\ObjectId($userid)],['web_links'=>['web_links.name'=>$name]],['$set'=>['web_links.$.status'=>$newstatus]]);
Here is the solution I finally got working. I sincerely hope it saves someone else consuming massively unhealthy amounts of coffee!

$this->database->users->updateOne(['_id'=>new MongoDB\BSON\ObjectId($userid),"web_links.name"=>$name],['$set' => ["web_links.$.status" => $newstatus]]);
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.