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("587af11ec09cf31a 1955ed92") ,
"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->up dateOne([' _id'=>new MongoDB\BSON\ObjectId($use rid)],['we b_links.na me'=>$name ],['$set'= >['web_lin ks.$.statu s'=>$newst atus]]);
This is returning the following error via firebug:
Uncaught exception 'MongoDB\Exception\Invalid ArgumentEx ception' 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
I also have a field/collection in some users called web_links which contains an array.
ie: within my users document:
{
"_id" : ObjectId("587af11ec09cf31a
"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:
},
{
"name" : "yahoo",
"link" : "https://yahoo.com",
"status" : "1",
"added" : ISODate("1970-01-18T08:00:
}
]
}
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->up
This is returning the following error via firebug:
Uncaught exception 'MongoDB\Exception\Invalid
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
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->up dateOne([' _id'=>new MongoDB\BSON\ObjectId($use rid)],['$p ull'=>['we b_links'=> ['name'=>$ name]]]);
I have sorted out the delete. Ill post incase it helps anyone :)
$this->database->users->up
ASKER
We're moving forward :)
different error to sort on the array update (the delete above is fine):
Uncaught exception 'MongoDB\Driver\Exception\ InvalidArg umentExcep tion' with message
'Replacement document may not contain "$" or "." in keys
I cant see any superfluous apostrophes, but thinking hard!
Thanks again
different error to sort on the array update (the delete above is fine):
Uncaught exception 'MongoDB\Driver\Exception\
'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
or use " instead
ASKER
will have a try , thanks Huseyin
ASKER
unfortunately no good on that one. Without quotes wouldn't run. Single to double quotes no different . Thanks
ASKER
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->re placeOne([ '_id'=>new MongoDB\BSON\ObjectId($use rid)],['we b_links'=> ['web_link s.name'=>$ name]],['$ set'=>['we b_links.$. status'=>$ newstatus] ]);
$this->database->users->re
ASKER
Here is the solution I finally got working. I sincerely hope it saves someone else consuming massively unhealthy amounts of coffee!
$this->database->users->up dateOne([' _id'=>new MongoDB\BSON\ObjectId($use rid),"web_ links.name "=>$name], ['$set' => ["web_links.$.status" => $newstatus]]);
$this->database->users->up
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers 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.
check this
https://github.com/mongodb/specifications/blob/master/source/crud/crud.rst#update-vs-replace-validation