MongoDB (2)

Source: Internet
Author: User
Tags bulk insert modifier

Create, update, and delete documents

1. Insert and Save:

1). Single INSERT, Insert:db.foo.insert ({"Bar": "Baz"})

2). Bulk INSERT, BatchInsert:db.foo.insert ([{"_id": 0},{"_id": 1}])

3). If you import only raw data (for example, from a data feed or from MySQL), you can use command-line tools such as Mongoimport instead of bulk INSERT.

4). If an error is encountered in the bulk INSERT, this document and all subsequent document insertions fail. You can use the ContinueOnError option to ignore the error and continue with subsequent insertions.

5). The maximum message length that the current version of MongoDB can receive is 48M

6). Insert Check, main verify there is no "_id", whether a single document is less than 16M

2. Deleting a document

1). Delete all documents in the collection: Db.foo.remove ()

2). Delete the document that meets the criteria: Db.mailing.llist.remove ({"Out": true})

3). Deleting the entire collection directly with drop is faster: Db.foo.drop

3. Update the document

1). UPDATE: Update has two parameters, the first is the query document for locating the target document to be updated, and the other is a modifier document that describes what modifications are made to the found document.

2). Update multiple documents: By default, the update only performs an action on the first document that matches the matching criteria. If more than one document meets the criteria, only the first document will be updated. To update all matching documents, you can change the update

The fourth parameter is set to True. The default behavior of update may change later, all eligible documents are updated by default, and only one is updated if the fourth parameter is set to False, so it is recommended that you explicitly

Indicates that you want to do more document updates.

3). To find out how many documents are updated in the multi-document update, you can run the GetLastError command.

4). The updated document can be obtained through the findandmofify command. Returns the data that was before the modification. Findandmodify can use the "Update" key or the "remove" key. Findandmofify has a number of fields that you can use:

Findandmodify: String, collection name

Query: Retrieving criteria for a document

Sort: Sorting result criteria

Udpate: Used to update a document

Remove: Indicates whether to delete the document, Boolean type

NEW: Boolean type that indicates whether to return the document before the update or the updated document. The default is the pre-update document.

Fields: The field that the document needs to return

Upert: Boolean type. A value of TRUE indicates that this is a upsert. The default is False.

Update and remove must have one, and only one. If there is no matching document, this command returns an error.

4. Modifier:

1). $set: $set used to specify a value for a field. If this field does not exist, it is created: Db.users.update ({"_id": ObjectId ("* * *")},... {"$set": {"name": "Lisi"}})

2). $unset: Remove a key completely

3). $inc: Increase the existing key value, or the key does not exist then create one. "$inc" can only be used for integers, long integers, or double-precision floating-point values

4). $push: Add an element to the end of the existing data and create one if it is not.

5). $each: Using the $each sub-operator, you can add multiple values with one $push operation

6). $slice: Use the $slice and $push combinations to ensure that the array does not exceed the set maximum length, which in effect gets an array of up to n elements. The $slice value must be a negative integer containing the last n elements added.

7). $sort: Sort. Note that you cannot use only $slice or $sort with $push and must be used with $each

8). $ne: No Duplicates

9). $addToSet: Avoid repeating values inserted into the collection. Combining $addtoset and $each, you can add multiple different values. Similar to set properties

$pop: Removes the element from the array. {"$pop": {"key": 1}} Deletes an element from the end of the array, {"$pop": {"key":-1}} removes an element from the head

$pull: You can use $pull to delete elements based on specific criteria, not just one by one element positions. $pull will delete all matching elements.

12). Location-based data modifier: by location or by positioning operator ("$"). A locator only updates the first matching element.

13). Modifier Speed: $inc can be modified in-place, because there is no need to change the size of the document, just to change the value of the key, so very fast. The data modifier can change the size of the document, which can be slower.

14). Fill factor: The fill factor is the growth space that MongoDB reserves for each document.

15). Moving documents is very slow. MongoDB must free up the space that the document originally occupied, and then write the document to another space.

16). You can use the usepowerof2sizes option to increase the disk reuse rate if your patterns are moving a lot or often disrupt data while inserting and deleting them. This option can be set by the Collmod command:

Db.runcommond ({"Collmod": CollectionName, "usepowerof2sizes": true})

When all spatial allocations are made after this collection, the resulting fast size is a power of 2. This option may cause the initial space allocation to be less efficient.

). Upsert: If a document that meets the update criteria is not found, a new document is created based on this condition and the updated document. If found, the update is normal. The third parameter of Udpate indicates that this is a upsert.

Db.analytics.update ({"url": "/blog"},{"$inc": {"pageviews": 1}},true)

This line of code is more efficient and atomic.

$setOnInsert: The value of the field is set only when the document is inserted. After all the update operations, the value of this field will no longer change.

Save Shell Helper: Save is a shell function that automatically creates a document if it does not exist, and updates the document if it exists.

5. Write a security mechanism

1). By default, inserts, deletes, and updates wait until the database responds (if the write succeeds) before continuing. Typically, when an error is encountered, the client throws an exception.

2). The answer write is the default way: The database gives a response that tells you whether the write operation was executed successfully. Non-responsive writes do not return any response, so there is no way to know if the write was successful.

3). When the shell performs a non-responsive write, it checks whether the last operation was successful. You can call GetLastError to manually force a check in the shell, which checks for errors in the last operation.

MongoDB (2)

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.