From 7fee719574d00d7831906420dfc12e631f585575 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sat, 28 Sep 2019 22:24:36 -0700 Subject: [PATCH] docs(documents): add overwriting section Fix #8178 --- docs/documents.pug | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/documents.pug b/docs/documents.pug index 0bff0166c44..b452fcce5ed 100644 --- a/docs/documents.pug +++ b/docs/documents.pug @@ -142,6 +142,28 @@ block content Read the [validation](./validation.html) guide for more details. + ### Overwriting + + There are 2 different ways to overwrite a document (replacing all keys in the + document). One way is to use the + [`Document#overwrite()` function](/docs/api/document.html#document_Document-overwrite) + followed by `save()`. + + ```javascript + const doc = await Person.findOne({ _id }); + + // Sets `name` and unsets all other properties + doc.overwrite({ name: 'Jean-Luc Picard' }); + await doc.save(); + ``` + + The other way is to use [`Model.replaceOne()`](/docs/api/model.html#model_Model.replaceOne). + + ```javascript + // Sets `name` and unsets all other properties + await Person.replaceOne({ _id }, { name: 'Jean-Luc Picard' }); + ``` + ### Next Up Now that we've covered Documents, let's take a look at