- given the document like so
{
"_id": "abc123",
"name": "Jesse",
"username": "jchappell",
"location": "New York",
"score": 23
}- Now I'd just like to update the
locationto "San Francisco"
myCollection.updateById("123abc", [
"location": "San Francisco"
]){
"_id": "abc123",
"name": "Jesse",
"username": "jchappell",
"location": "New York",
"score": 23
}Setting with a dictionary like ["location": "San Francisco"] is equivalent to giving it an an array of UpdateOperation that are of the .set case variant.
myCollection.updateById("123abc", [
UpdateOperation.set("location", "San Francisco")
])So let's say you want to do 3 things in 1 update command
- Get rid of the
"name"key - Change
locationto"Paris" - Change
usernameto"jesse_chappellXYZ"
You can do this:
let operations: [UpdateOperation] [
.unset("name", ""),
.set("location", "Paris"),
.set("username", "jesse_chappellXYZ"),
]
myCollection.updateById("123abc", operations)The document will now go from:
{
"_id": "abc123",
"name": "Jesse",
"username": "jchappell",
"location": "New York",
"score": 23
}to:
{
"_id": "abc123",
"username": "jesse_chappellXYZ",
"location": "Paris",
"score": 23
}