MongoDB Operations Manual CRUD Update update

Source: Internet
Author: User

Modify a record

OverviewMongoDB provides the update () method for updating records. This method accepts the following parameters:
A JSON object that updates the criteria for matching records, an update action JSON object for declaring the update operation, and an option for the JSON object
declares query criteria, using the same structure and syntax as querying.
By default, update () updates a single record to update more than one record, using the multi option.
update a specified field in a record used to update a value of a field, MongoDB provides an update operator, such as $set.
When performing an update operation, some operators back to create fields that are not, such as $set.
test data:Db.testData.insert ({item: ' MON2 '});
1. Update field values using the update operator             For a record, where the item field value is MNO2, use the $set operator to update its category and details fields, and use the $currentdate operator to update the LastModified field
            Db.testData.update (
                    {item: ' MNO2 '},
                    {
                        $set: {
                            Category: ' Apparel ',
                            Details:{model: ' 14q3 ', Manufacturer: ' XYZ company '}
                        },
                        $currentDate: {lastmodified:true}
                    }
                );
                This update returns the Writeresult object that contains the state of the operation.
                A successful update operation returns the following results:
                    Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
                    Nmatched Total segment indicates the number of records matched, and the Nmodified field indicates the number of records modified.
2. Updating inline fields use the ". " operator, and enclose the attribute in quotation marks.

Example: Updating the Details field of an embedded model

db.testData.update ({item: ' ABC1 '},{$set: {' Details.model ': ' 14q2 '}});

3. Update multiple records Update the category value for all records that contain category with the value "clothing" to "apparel", lastmodified field is the current time
Db.testData.update (
{category: ' Clothing '},
{
$set: {category: ' Apparel '},
$currentDate: {lastmodified:true}
},
{Multi:true}
)
Replace recordIn addition to the _id field, replacing all the contents of a record requires that the new entire record's object be passed in as the second parameter of the update () method.
The replaced record can have a different field than the previous record, and because the _id field is immutable, the _id field can be omitted from the replacement record. If you do not want to include the field, you must be a value that exists in the collection.
Example: Replace the item field with a record of BE10. When replaced, the new record will contain only the fields in the _id field and the replacement record.
Db.testData.update (
{item: "BE10"},
{
Item: "BE05",
Stock: [{size: ' S ', qty:20}, {size: ' M ', qty:5}],
Category: "Apparel"
}
)
Upsert OptionsOverview

By default, if there are no matching records in the update () method, this method will not take any action.

However, if Upsert:true is declared, it will be updated when there are matching records, and inserts will be made when there is no match.
1. Use Upsert when replacing records         When you replace a record with the update operation, declare Upsert:true, and if there are no matching records, MongoDB will use the    The query condition creates a new record and then replaces everything except the _id field in the newly created record with the record used in the update for replacement.
            Db.testData.update (
                {item: "TBD1"},
                {
                    Item: "TBD1",
                    Details: {"model": "14q4", "Manufacturer": "ABC Company"},
                   Stock: [{"Size": "S", "Qty": 25}],
                    Category: "Houseware"
                },
                {Upsert:true}
            )
Results: nupserted ": 1 indicates
Writeresult ({
"nmatched": 0,//No matching record
"nupserted": 1,//inserts a new record
"Nmodified": 0,//no record updated
"_id": ObjectId ("53DBD684BABEAEC6342ED6C7")//_id of newly inserted records
})
2.use Upsert when updating recordsWhen updating a record using the update operation, declare upsert:true, as above
            Db.testData.update (
                {item: "TBD2"},
                {
                    $set: {
                        Details: {"model": "14q3", "Manufacturer": "IJK Co."},
                        Category: "Houseware"
                    }
                },
                {Upsert:true}
            )

MongoDB Operations Manual CRUD Update update

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.