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.

TigrisCollection<Catalog> catalog = tigrisDatabase.getCollection(Catalog.class);

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

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}

Simple Update

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.

UpdateResponse response = catalog.update(
Filters.eq("id", 1),
UpdateFields.newBuilder().set("price", 65).build()

Updates using logical OR

A single update is shown in the above example but there could be a need to update multiple products by their primary key. A logical OR filter is one of the ways of updating multiple documents. The example below is updating the popularity score for two documents that have id 1 or 4.

Filters.eq("id", 1),
Filters.eq("id", 4)),
UpdateFields.newBuilder().set("popularity", 9).build()

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.

Filters.eq("id", 4),
Filters.eq("id", 2)),
UpdateFields.newBuilder().set("popularity", 9).build()

More filters can be used to narrow down the results further.

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.

Filters.eq("name", "sneakers shoes"),
Filters.eq("name", "adidas")),
UpdateFields.newBuilder().set("price", 85).build()