The difference between the MongoDB save () method and the Insert () method

Source: Internet
Author: User
Tags mongodb mongodb client mongodb save mongo shell

The difference between the MongoDB save () method and the Insert () method first look at what the official documents say

Updates an existing document or inserts a new document, depending on its document parameter

The Save method has both the ability to update and insert, and whether to insert or update the document depends on the save parameter. So what parameters do you depend on? Keep looking.

If The document does not contain a _id field, then the Save () method calls the Insert () method. During the operation, the MONGO Shell would create an ObjectId and assign it to the _id field.

You can see whether the decision is to insert a document or update, depending on the _id parameter. If you can find a document that already exists according to _ID, then update it. If you do not pass in the _id parameter or you cannot find a document that exists, insert a new document.

To cite an official example

With no _id parameters

Db.products.save ({item: "Book", Qty:40})

Results

{"_id": ObjectId ("50691737d386d8fadbd6b01d"), "Item": "Book", "Qty": 40}

The MongoDB client driver will automatically generate a default for you
Objectid as _id.

With _id parameter, but cannot find a document that already exists

Db.products.save ({_id:100, item: "Water", qty:30})

Results

{"_id": +, "item": "Water", "qty": 30}

or insert a new document, but _id is not automatically generated.

With _id parameter, but there is a document that exists

Db.products.save ({_id:100, item: "Juice"})

Results

{"_id": +, "item": "Juice"}

Updated the document

Summarize
    1. Insert: If the primary key of the new data already exists, the org.springframework.dao.DuplicateKeyException exception will be thrown to repeat the primary key, not saving the current data.
    2. Save: If the primary key for the new data already exists, the existing data will be modified.

The difference between the MongoDB save () method and the Insert () method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.