Skip to main content

Update Documents

The Update API is used to update existing documents that match the filters. Fields that need to be updated are specified when calling the API.

Example collection

The first step is to set up the collection object. All the operations on the collection is performed through this collection object.

const catalog = db.getCollection<Catalog>("catalog");

Now let's assume an e-commerce website that has the above collection catalog and has 5 products(documents) present in it.

idnamepricebrandlabelspopularityreviews
1fiona handbag99.9michael korspurses8{"author": "alice", "rating": 7}
2tote bag49coachhandbags9{"author": "olivia", "rating": 8.3}
3sling bag75coachpurses9{"author": "alice", "rating": 9.2}
4sneakers shoes40adidasshoes10{"author": "olivia", "rating": 9}
5running shoes89nikeshoes10{"author": "olivia", "rating": 8.5}

Update one

A simple update is by updating a document that matches the id which is the primary key of this collection. In the above collection, let's say there is a need to modify the price of a product that has an id assigned as 1.

const updateResponse = await catalog.updateOne({
filter: { id: 1 },
fields: { price: 65}, // set the price of this product to 65
});

Update many

The update many operation is used to update multiple documents. A common approach to achieving this is by using the logical OR filter. The example below is updating the popularity score for two documents that have id 1 or 4.

const updateResponse = await catalog.updateMany({
filter: {
op: LogicalOperator.OR,
selectorFilters: [
{ id: 1 },
{ id: 4 }
]
},
fields: {
popularity: 9,
}
});

Range update

Extending the example, what if there is a need to reset the popularity score of a few products but these product ids are continuous? In this case, a range update can be performed on this field. The following example is applying range update on the id field.

const updateResponse = await catalog.updateMany({
filter: {
op: LogicalOperator.AND,
selectorFilters: [
{
op: SelectorFilterOperator.LT,
fields: { id: 4 }
},
{
op: SelectorFilterOperator.GTE,
fields: { id: 2 }
}
]
},
fields: {
popularity: 9
}
});

Filtering on multiple fields

There may be cases when you don't know the primary key value and instead wants to update a document by applying a condition on the non-primary key field. The following example is increasing the price of a product when the name and brand match with the documents present in the collection.

const updateResponse = await catalog.updateMany({
filter: {
op: LogicalOperator.AND,
selectorFilters: [
{
name: "sneakers shoes"
},
{
brand: "adidas"
}
]
},
fields: {
price: 85,
}
});

Increment a field value

The following example demonstrates incrementing field value

const updateResponse = await catalog.updateOne({
filter: { id: 1 },
fields: { $increment: { popularity: 1 } }, // increment the value of the field popularity by 1
});

Decrement a field value

The following example demonstrates decrementing field value

const updateResponse = await catalog.updateOne({
filter: { id: 1 },
fields: { $decrement: { popularity: 1 } }, // decrement the value of the field popularity by 1
});

Multiply field value

The following example demonstrates multiplying field value

const updateResponse = await catalog.updateOne({
filter: { id: 1 },
fields: { $multiply: { popularity: 2 } }, // multiply the value of the field popularity by 2
});

Divide field value

The following example demonstrates dividing field value

const updateResponse = await catalog.updateOne({
filter: { id: 1 },
fields: { $divide: { popularity: 2 } }, // divide the value of the field popularity by 2
});