MongoDB Documentation, Release 3.0.4 Step 1: Specify upsert: true for the update replacement operation. Step 2: Specify upsert: true for the update specific fields operation. db.inventory.update( { item: "TBD2" }, { $set: { details: { "model" : "14Q3", "manufacturer" : "IJK Co." }, category: "houseware" } }, { upsert: true } ) WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : ObjectId("53dbd684babeaec6342ed6c7") }) db.inventory.update( { item: "TBD1" }, { item: "TBD1", details: { "model" : "14Q4", "manufacturer" : "ABC Company" }, stock: [ { "size" : "S", "qty" : 25 } ], category: "houseware" }, { upsert: true } ) When you specify upsert: true for an update operation to replace a document and no matching documents are found, MongoDB creates a new document using the equality conditions in the update conditions document, and replaces this document, except for the _id field if specified, with the update document. The following operation either updates a matching document by replacing it with a new document or adds a new document if no matching document exists. The update operation returns a WriteResult object which contains the status of the operation, including whether the db.collection.update() method modified an existing document or added a new document. The nMatched field shows that the operation matched 0 documents. The nUpserted of 1 shows that the update added a document. The nModified of 0 specifies that no existing documents were updated. The _id field shows the generated _id field for the added document. When you specify upsert: true for an update operation that modifies specific fields and no matching documents are found, MongoDB creates a new document using the equality conditions in the update conditions document, and applies the modification as specified in the update document. The following update operation either updates specific fields of a matching document or adds a new document if no matching document exists. 105 3.3. MongoDB CRUD Tutorials