PHP MongoDB, refresh the document without erasing the rest

I am trying to update a mongo file using PHP and the update () function. However, when I do this, it replaces the WHOLE document with the value that I wanted to update. How can i fix this?

The code I have written so far: http://twaddlr.com/view/73 (Scroll down to the "update" function. This is the database shell that I write for my site)

+6
source share
1 answer

The key is to use $set in the update, for example. instead (sorry, using the JavaScript syntax here, not sure about the exact syntax of the PHP driver):

 db.my_collection.update({hello: "world"}, {foo: "bar"}) 

you do

 db.my_collection.update({hello: "world"}, {$set: {foo: "bar"}}) 

If you use $set , only the properties you specify will be updated, the entire document will not be replaced.

You can read more about this in the documentation: http://www.mongodb.org/display/DOCS/Updating#Updating-ModifierOperations

Edit: looking at your code, this is exactly what you do in the addRow method. Just do the same in update .

+16
source

Source: https://habr.com/ru/post/885658/


All Articles