Delete Documents
Delete API is used to delete existing documents that match the filters.
Example collection
The first step is to set up the collection object. All the operations on the collection are performed through this collection object.
- Sync
- Async
TigrisCollection<Catalog> catalog = tigrisDatabase.getCollection(Catalog.class);
TigrisAsyncCollection<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.
id | name | price | brand | labels | popularity | reviews |
---|---|---|---|---|---|---|
1 | fiona handbag | 99.9 | michael kors | purses | 8 | {"author": "alice", "rating": 7} |
2 | tote bag | 49 | coach | handbags | 9 | {"author": "olivia", "rating": 8.3} |
3 | sling bag | 75 | coach | purses | 9 | {"author": "alice", "rating": 9.2} |
4 | sneakers shoes | 40 | adidas | shoes | 10 | {"author": "olivia", "rating": 9} |
5 | running shoes | 89 | nike | shoes | 10 | {"author": "olivia", "rating": 8.5} |
Delete one
A simple delete is by filtering on the primary key of the document. Let's say you need to remove a product
from this collection that has an id
assigned as 5.
- Sync
- Async
DeleteResponse response = catalog.delete(Filters.eq("id", 5));
CompletableFuture<DeleteResponse> future = catalog.delete(Filters.eq("id", 5));
Delete many
Extending single document delete example, in case the products that you are planning to remove have continuous ids then you can
use range delete to remove these products. As an example, if you need to delete documents that have id
greater
than or equal to 3 but less than 5.
catalog.delete(
Filters.and(
Filters.lt("id", 5),
Filters.gte("id", 3)
)
);
Delete All
Sometimes, there is a need to delete all the documents then an empty filter can be sent to the server.
catalog.delete(Filters.nothing());