Navigation
This version of the documentation is archived and no longer supported.

db.collection.save()

db.collection.save(document)

The save() method updates an existing document or inserts a document depending on the parameter.

The save() method takes the following parameter:

Parameters:
  • document

    Specify a document to save to the collection.

    If the document does not contain an _id field, then the save() method performs an insert with the specified fields in the document as well as an _id field with a unique objectid value.

    If the document contains an _id field, then the save() method performs an upsert querying the collection on the _id field:

    • If a document does not exist with the specified _id value, the save() method performs an insert with the specified fields in the document.
    • If a document exists with the specified _id value, the save() method performs an update, replacing all field in the existing record with the fields from the document.

Consider the following examples of the save() method:

  • Pass to the save() method a document without an _id field, so that to insert the document into the collection and have MongoDB generate the unique _id as in the following:

    db.products.save( { item: "book", qty: 40 } )
    

    This operation inserts a new document into the products collection with the item field set to book, the qty field set to 40, and the _id field set to a unique ObjectId:

    { "_id" : ObjectId("50691737d386d8fadbd6b01d"), "item" : "book", "qty" : 40 }
    

    Note

    Most MongoDB driver clients will include the _id field and generate an ObjectId before sending the insert operation to MongoDB; however, if the client sends a document without an _id field, the mongod will add the _id field and generate the ObjectId.

  • Pass to the save() method a document with an _id field that holds a value that does not exist in the collection to insert the document with that value in the _id value into the collection, as in the following:

    db.products.save( { _id: 100, item: "water", qty: 30 } )
    

    This operation inserts a new document into the products collection with the _id field set to 100, the item field set to water, and the field qty set to 30:

    { "_id" : 100, "item" : "water", "qty" : 30 }
    

    Note

    Most MongoDB driver clients will include the _id field and generate an ObjectId before sending the insert operation to MongoDB; however, if the client sends a document without an _id field, the mongod will add the _id field and generate the ObjectId.

  • Pass to the save() method a document with the _id field set to a value in the collection to replace all fields and values of the matching document with the new fields and values, as in the following:

    db.products.save( { _id:100, item:"juice" } )
    

    This operation replaces the existing document with a value of 100 in the _id field. The updated document will resemble the following:

    { "_id" : 100, "item" : "juice" }