Rechercher dans le manuel MySQL
20.3.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-js> 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-js> db.countryinfo.modify("Name = 'Sealand'").unset("GNP")
Query OK, 1 item affected (0.01 sec)
Use the find()
method to verify the change.
mysql-js> 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
arrayAppend()
,
arrayInsert()
, or
arrayDelete()
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-js> db.countryinfo.modify("true").set("Airports", [])
Query OK, 240 items affected (0.07 sec)
With the Airports field added, the next example uses the
arrayAppend()
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-js> db.countryinfo.modify("Name = 'France'").arrayAppend("$.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 arrayInsert()
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-js> db.countryinfo.modify("Name = 'France'").arrayInsert("$.Airports[0]", "CDG")
Query OK, 1 item affected (0.04 sec)
To delete an element from the array, you must pass to the
arrayDelete()
method the index of the
element to be deleted.
mysql-js> db.countryinfo.modify("Name = 'France'").arrayDelete("$.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.
Traduction non disponible
Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-mysql-shell-tutorial-javascript-documents-modify.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.