I am creating a lot of documents, and I am concerned that half of the server bandwidth is being spent on returning those new documents back to the caller.
I don't want the server to return the document to me. I just want to get an acknowledgement once the document has been saved.
I have been able to address this concern when updating a document, by using Model.updateOne()
instead of Model.findOneAndUpdate()
.
But how can I do the same when creating a document?
So far I have tried:
Model.create(docData)
new Model(docData).save()
Model.collection.insert(docData)
Model.collection.insertMany([docData])
but in every case, mongoose (or the mongodb driver) returns a document to me.
I am not sure if MongoDB is actually sending the document back over the network, or if it is just sending back the new _id
and that is being appended to the original data by the driver. But I fear the former.
How can I save the document without MongoDB sending it back to me?
According to mongoose document of insertMany it says that the result is an insertWriteOpCallback which, according to the documentation of mongodb driver says that it return the inserted documents.
In other hands the mongodb documentation about insert tells us about a WriteResult object that is not returning any document.
The answer is that mongodb-driver is adding the inserted document into it's response.
What you can do is to use the method bulkWrite from mongodb-native driver, which allow you to perform an insert and get the mongodb response only (so without documents).