Documentation Home
MySQL 9.3 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 40.8Mb
PDF (A4) - 40.9Mb
Man Pages (TGZ) - 261.1Kb
Man Pages (Zip) - 368.3Kb
Info (Gzip) - 4.1Mb
Info (Zip) - 4.1Mb


22.3.3.4 Modify Documents

You can use themodify() method to update one or more documents in a collection. The X DevAPI provides additional methods for use with themodify() 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 Document Fields

Themodify() 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, themodify() method uses the search condition to identify the document to change and then theset() method replaces two values within the nested demographics object.

mysql-js> db.countryinfo.modify("Code = 'SEA'").set("demographics", {"LifeExpectancy": 78, "Population": 28})

After you modify a document, use thefind() method to verify the change.

To remove content from a document, use themodify() andunset() 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")

Use thefind() method to verify the change.

mysql-js> db.countryinfo.find("Name = 'Sealand'"){    "_id": "00005e2ff4af00000000000000f4",    "Name": "Sealand",    "Code:": "SEA",    "IndepYear": 1967,    "geography": {        "Region": "British Islands",        "Continent": "Europe",        "SurfaceArea": 193    },    "government": {        "HeadOfState": "Michael Bates",        "GovernmentForm": "Monarchy"    },    "demographics": {        "Population": 27,        "LifeExpectancy": 79    }}
Append, Insert, and Delete Arrays

To append an element to an array field, or insert, or delete elements in an array, use thearrayAppend(),arrayInsert(), orarrayDelete() methods. The following examples modify thecountryinfo collection to enable tracking of international airports.

The first example uses themodify() andset() methods to create a new Airports field in all documents.

Caution

Use care when you modify documents without specifying a search condition; doing so modifies all documents in the collection.

mysql-js> db.countryinfo.modify("true").set("Airports", [])

With the Airports field added, the next example uses thearrayAppend() 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")

Usefind() to see the change.

mysql-js> db.countryinfo.find("Name = 'France'"){    "GNP": 1424285,    "_id": "00005de917d80000000000000048",    "Code": "FRA",    "Name": "France",    "Airports": [        "ORY"    ],    "IndepYear": 843,    "geography": {        "Region": "Western Europe",        "Continent": "Europe",        "SurfaceArea": 551500    },    "government": {        "HeadOfState": "Jacques Chirac",        "GovernmentForm": "Republic"    },    "demographics": {        "Population": 59225700,        "LifeExpectancy": 78.80000305175781    }}

To insert an element at a different position in the array, use thearrayInsert() 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")

To delete an element from the array, you must pass to thearrayDelete() method the index of the element to be deleted.

mysql-js> db.countryinfo.modify("Name = 'France'").arrayDelete("$.Airports[1]")
Related Information