MongoDB (iii): MongoDB additions and deletions (1)

Source: Internet
Author: User
Tags bulk insert modifier

Before learning MongoDB, it is better to first understand the JSON data format, MongoDB Bson is an extension of JSON, and JSON, if you understand the JSON, it is easy to learn.

And, of course, JSON is easy to get started with.

(1): Insert can have two types, single insert document and BULK INSERT document

A. Inserting a document: Example: Db.persons.insert ({"_id": "001", "Name": "Zhangsan", "Age": 23})

Description: It can be understood to be added in JSON format, FindOne () is the first record of the query.

B. Bulk insert: To put it bluntly, insert an array:, as follows:

Description: In the old version it seems that bulk insert is not supported, but the new version has been supported, in the old version, you can write a For loop implementation of the batch increase, since the new version already has

Increase in volume, use this method.

(2) Save operation: The difference between the save and insert operations is that when encountering _id the same situation, save will follow the new action on that data, and insert will give an error. As follows:

(3) Remove operation: delete data. Remove all without conditions: db.persons.remove (). Write a conditional delete below: Db.persons.remove ({"_id": "003"}), as follows:

(4): Update operation: The modifier in the update operation will be in the next section, say here: Tough document replacement update operation, plainly, is to delete the previous record, and then add a new

(can also be described as an overall update), as follows: Db.persons.update ({"_id": "001"}, {"Age": 22})

Description: A.update method Syntax: Update (query condition, update result); < actually does not stop two parameters, the latter will be exposed to >

B. As can be seen from the above results, although the age of this field has been updated, but the name field is not, this is a tough document replacement update operation,

is to use this new article replace the original one. The modifier described later is a partial update.

C. Note: The primary key conflict will error and stop the update operation, because it is hard to replace when the replacement of the document and the existing document ID conflict, the system will error

(5): Upsert operation: As the name implies, this is the abbreviation for two words: Update and INSERT, the purpose of this operation is if there is this data, the update operation, if there is no data

the insert operation is performed. But notice, actually does not upsert this method, Upsert is only one kind of argument, its realization still relies on the update method to do. The Update method's

The third parameter is changed to TRUE to achieve upsert. as follows: Db.persons.update ({"_id": "003"},{"name": "Wangwu"},true)

From the results can be seen, the data to be updated in the original data does not exist, all added a piece of data, but the _id value is automatically generated, can not be specified (here is a bit tangled, why

Don't you want my _id? )。

(6): Bulk update operation, the default is only updated to meet the first rule, even if there are more than one data to meet the query criteria, will only update the first data, as follows:

How do you do all the updating? Change the fourth parameter of the Update method to True, as follows: Db.persons ({"Name": "Wangwu"},{"name": "Zhaoliu"},false,true)

Note: A mistake was made at the beginning: Multi update only works with $ operators, meaning that if a batch update is used, only local updates can be made, and

cannot be overall update. $set is a modifier that is part of the update operation. You can think of it as a special key (always keep in mind the JSON format will not faint). I'll talk about it in the next chapter

Modifier.

MongoDB (iii): MongoDB additions and deletions (1)

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.