Reputation: 623
I have document like this:
{
"_id" : ObjectId("591ed2f0470e6ccc143c986e"),
"name" : "Planets",
"prototype_id" : null,
"parameters" : [
"591eefe3470e6cd70c3c9872",
"591eefc3470e6c500f3c9872",
"591eedbe470e6cd70c3c9871"
],
"available" : "1"
}
I tried to set []
for field parameters
if value 591eefe3470e6cd70c3c9872
exists in this array.
I tried:
$new = array('$set' => array("parameters" => []));
$this->collection->update(array("parameters" => "591eedbe470e6cd70c3c9871"), $new);
It does not work...
Upvotes: 0
Views: 65
Reputation: 1990
MongoDB's update() and save() methods are used to update document into a collection. The update() method updates the values in the existing document while the save() method replaces the existing document with the document passed in save() method.
MongoDB Update() Method
The update() method updates the values in the existing document.
Syntax
The basic syntax of update() method is as follows −
db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)
Consider the mycol collection has the following data
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"My Overview"}
Following example will set the new title 'New MongoDB Tutorial' of the documents whose title is 'MongoDB Overview'.
db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}})
db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"New MongoDB Tutorial"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"My Overview"}
By default, MongoDB will update only a single document. To update multiple documents, you need to set a parameter 'multi' to true.
db.mycol.update({'title':'MongoDB Overview'},
{$set:{'title':'New MongoDB Tutorial'}},{multi:true})
Upvotes: 1