I am exploring the capabilities RESTHeart offers for interacting with MongoDB.
Is there a way to perform a bulkWrite
operation? I’m referring to the MongoDB bulkWrite
method, which allows a single batch operation to include a mix of different types of operations (like insertOne
, updateOne
, updateMany
, replaceOne
, deleteOne
, and deleteMany
) as shown in the example below:
db.collection.bulkWrite(
[
{ insertOne: <document> },
{ updateOne: <document> },
{ updateMany: <document> },
{ replaceOne: <document> },
{ deleteOne: <document> },
{ deleteMany: <document> }
],
{ ordered: false }
)
In the RESTHeart documentation, it mentions bulk write capabilities, but it seems to primarily refer to operations like insertMany
or updateMany
.
Would it be a good idea to write a RESTHeart plugin in Java to enable true bulk write functionality?
I tried what’s here in the docs.
Filip Vercauteren is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
The RESTHeart MongoDB plugin provides a RESTful API to interact with data, where each URL identifies a resource, and the HTTP verb specifies the operation to be performed.
To perform bulk write operations on documents, use the following request:
POST /coll [ {}, {}, {} ]
The type of MongoDB operation is determined by the ?wm=insert/update/upsert
query parameter:
- UPDATE -> bulk write consisting of one
ReplaceOneModel
per document in the request array - UPSERT -> bulk write consisting of one
ReplaceOneModel
with upsert option per document in the request array - INSERT -> bulk write consisting of one
InsertOneModel
per document in the request array
To bulk delete documents, use:
DELETE /coll/*?filter={ "qty": {"$gte": 10 } }
This operation leverages the DeleteManyModel
class.
To bulk update documents, use:
PATCH /coll/*?filter={ "qty": {"$lte": 10 } } { "a": 1, "b": { "$qty": 10 }}
This operation leverages the UpdateManyModel
class.
These requests, along with the single-document operations POST /coll {}
, PATCH /coll/docid
, and DELETE /coll/docid
, cover 99% of common use cases.
For more specialized needs, a custom plugin can be implemented. For example, you could create a plugin that accepts an array of _ids to bulk delete documents using DeleteManyModel
.