$unset
The $unset operator in Amazon DocumentDB is used to remove a specified field from a document. When a field is removed using $unset, the field is deleted from the document, and the document size is reduced accordingly. This can be useful when you want to remove unnecessary data from your documents.
Parameters
-
field: The field to remove from the document. This can be a single field or a dotted path to a nested field.
Example (MongoDB Shell)
The following example demonstrates how to use the $unset operator to remove the Words field from a document in the example collection.
Create sample documents
db.example.insert({ "DocName": "Document 1", "Date": { "Month": 4, "Day": 18, "Year": 1987, "DoW": "Saturday" }, "Words": 2482 })
Query example
db.example.update( { "DocName" : "Document 1" }, { $unset: { Words:1 } } )
Output
{
"DocName": "Document 1",
"Date": {
"Month": 4,
"Day": 18,
"Year": 1987,
"DoW": "Saturday"
}
}
In this example, the $unset operator is used to remove the Words field from the document with DocName equal to "Document 1". The resulting document no longer contains the Words field.
Code examples
To view a code example for using the $unset command, choose the tab for the language that you want to use: