Rechercher dans le manuel MySQL
20.4.3.4 Modify Documents
You can use the modify()
method to update one
or more documents in a collection. The X DevAPI provides
additional methods for use with the modify()
method to:
Set and unset fields within documents.
Append, insert, and delete arrays.
Bind, limit, and sort the documents to be modified.
Set and Unset Fields
The modify()
method works by filtering a
collection to include only the documents to be modified and
then applying the operations that you specify to those
documents.
In the following example, the modify()
method uses the search condition to identify the document to
change and then the set()
method replaces
two values within the nested demographics object.
mysql-py> db.countryinfo.modify("_id = 'SEA'").\
set("demographics", {"LifeExpectancy": 78, "Population": 28})
Query OK, 1 item affected (0.04 sec)
After you modify a document, use the find()
method to verify the change.
To remove content from a document, use the
modify()
and unset()
methods. For example, the following query removes the GNP from
a document that matches the search condition.
mysql-py> db.countryinfo.modify("Name = 'Sealand'").unset("GNP")
Query OK, 1 item affected (0.01 sec)
Use the find()
method to verify the change.
mysql-py> db.countryinfo.find("Name = 'Sealand'")
[
{
"IndepYear": 1967,
"Name": "Sealand",
"_id": "SEA",
"demographics": {
"LifeExpectancy": 78,
"Population": 28
},
"geography": {
"Continent": "Europe",
"Region": "British Islands",
"SurfaceArea": 193
},
"government": {
"GovernmentForm": "Monarchy",
"HeadOfState": "Michael Bates"
}
}
]
1 document in set (0.00 sec)
To append an element to an array field, or insert, or delete
elements in an array, use the
array_append()
,
array_insert()
, or
array_delete()
methods. The following
examples modify the countryinfo
collection
to enable tracking of international airports.
The first example uses the modify()
and
set()
methods to create a new Airports
field in all documents.
Use care when you modify documents without specifying a search condition. This action will modify all documents in the collection.
mysql-py> db.countryinfo.modify(True).set("Airports", [])
Query OK, 240 items affected (0.07 sec)
With the Airports field added, the next example uses the
array_append()
method to add a new airport
to one of the documents. $.Airports in
the following example represents the Airports field of the
current document.
mysql-py> db.countryinfo.modify("Name = 'France'").array_append("$.Airports", "ORY")
Query OK, 1 item affected (0.02 sec)
Use db.countryinfo.find("Name =
'France'")
to see the change.
To insert an element at a different position in the array, use
the array_insert()
method to specify which
index to insert in the path expression. In this case, the
index is 0, or the first element in the array.
mysql-py> db.countryinfo.modify("Name = 'France'").array_insert("$.Airports[0]", "CDG")
Query OK, 1 item affected (0.04 sec)
To delete an element from the array, you must pass to the
array_delete()
method the index of the
element to be deleted.
mysql-py> db.countryinfo.modify("Name = 'France'").array_delete("$.Airports[1]")
Query OK, 1 item affected (0.03 sec)
The MySQL Reference Manual provides instructions to help you search for and modify JSON values.
See CollectionModifyFunction for the full syntax definition.
Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-mysql-shell-tutorial-python-documents-modify.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.