MongoDB NOTE 2: Create, update, and delete documents

Source: Internet
Author: User
Chapter 3 create, update, and delete a document insert a new document: db. foo. insert ({bar: baz}) My understanding: database. set. insert ({key: value}) Note: The insert speed is faster than that of data inserted at a time, because batch insert only

Chapter 3 create, update, and delete a document insert a new document: db. foo. insert ({bar: baz}) My understanding: database. set. insert ({key: value}) Note: The insert speed is faster than that of data inserted at a time, because batch insert only

Chapter 3
Create, update, and delete documents

Insert new document:

Db. foo. insert ({"bar": "baz "})

My understanding: database. Set. insert ({key: value })

Note: The insert speed is faster than that of data inserted at a time, because batch insert is only a single TCP request, avoiding the overhead caused by many fragmented requests. (Single set)
The message length of MongoDB2.0 is 16 MB.

Process: After the insert operation is executed, the driver will convert the data into the BSON format and then send it to the database. The database will parse the BSON, check whether the "_ id" Key is included and the document size cannot exceed 4 MB,
Note: drivers in all mainstream languages check the validity of some data prior to data transfer (whether the document is too long, whether it contains UTF-8 characters, or uses an unknown type ). You can use the -- objcheck option when starting the database server. The server will check the validity of the document structure before inserting it.

Delete document:

Db. users. remove ()


Database. Set. Delete ()
Note: All documents in the users set will be deleted, but the set itself will not be deleted, and the original indexes will be retained.
The remove () function query accepts a query document as an optional parameter. For example

Db. users. remove ({"key": "world "})


Note: deleting data is permanent and cannot be undone or restored.

Updated document: update: two parameters, Hong Kong server, query document and modifier document.

Use modifier:
Generally, only one part of a document needs to be updated. The Atomic update modifier can make this update highly efficient. An update modifier is a special key used to specify complex update operations, such as adjustments, adding or deleting keys, or operating arrays or embedded documents.
$ Set
Add with conditional PD

> Db. users. insert ({"set": "set "})
>
>
> Db. users. find ()
{"_ Id": ObjectId ("502e9c960852475a6e43ba78"), "set": "set "}
>
>
> Db. users. update ({"_ id": ObjectId ("502e9c960852475a6e43ba78 ")},
... {"$ Set": {"hello2": "hello2 "}}
...)
> Db. users. find ()
{"_ Id": ObjectId ("502e9c960852475a6e43ba78"), "hello2": "hello2", "set": "set "}
> Db. users. update ({"_ id": ObjectId ("502e9c960852475a6e43ba78 ")},
... {"$ Set": {"nohello": "nohello "}})
> Db. users. find ()
{"_ Id": ObjectId ("502e9c960852475a6e43ba78"), "hello2": "hello2", "nohello": "nohello", "set": "set "}
> Db. users. update ({"hello2": "hello2 "},
... {"$ Set": {"a": ""}})
> Db. users. find ()
{"_ Id": ObjectId ("502e9c960852475a6e43ba78"), "a": "a", "hello2": "hello2", "nohello": "nohello ", "set": "set "}



$ Unset Delete

> Db. users. update ({"set": "set "},
... {"$ Unset": {"a": ""}})
> Db. users. find ()
{"_ Id": ObjectId ("502e9c960852475a6e43ba78"), "hello2": "hello2", "nohello": "nohello", "set": "set "}
Note: delete this key/value if only one PD value exists.
> Db. users. find ()
{"_ Id": ObjectId ("502e9c960852475a6e43ba78"), "hello2": "hello2", "nohello": "nohello", "set": "set "}
> Db. users. update ({"set": "set "},
... {"$ Unset": {"hello2": "nohello "}})
> Db. users. find ()
{"_ Id": ObjectId ("502e9c960852475a6e43ba78"), "nohello": "nohello", "set": "set "}



The documents in the same document also adapt to {"author. name ":"??? "}

You must use a modifier starting with $ to modify the key/value pair.

Increase and decrease:
$ Inc
View the value of afs. It can only be a number.

> Db. users. update ({"set": "set" },{ "$ inc": {"afs": 10 }})
> Db. users. find ()
{"_ Id": ObjectId ("502e9c960852475a6e43ba78"), "afs": 10, "nohello": "nohello", "set": "set "}
> Db. users. update ({"set": "set" },{ "$ inc": {"afs": 50 }})
> Db. users. find ()
{"_ Id": ObjectId ("502e9c960852475a6e43ba78"), "afs": 60, "nohello": "nohello", "set": "set "}
> Db. users. update ({"set": "set" },{ "$ inc": {"afs":-50 }})
> Db. users. find ()
{"_ Id": ObjectId ("502e9c960852475a6e43ba78"), "afs": 10, "nohello": "nohello", "set": "set "}


Array modifier:
$ Push
Add to the end if any, and create a new array if no.
Data operations can only be used on keys whose values are arrays. For example, you cannot push integers or POP strings, use "$ set" or "$ inc" to modify the scalar value.
The procedure is the same.
$ Ne $ addToSet avoid duplication

Array positioning Modifier


Upsert
It is a special update. If no document meets the update condition, a new document is created based on this condition and the updated document. If a matched document or website space is found, the upsert function is updated normally. You do not need to preset a collection. The same set of code can be used to create and update documents.

Save Shell help program
Save is a shell function that can be inserted when the document does not exist and updated when it exists. It has only one parameter: document. If this document contains the "_ id" Key, save Will call upsert. Otherwise, insert is called. Programmers can easily use this function to quickly modify documents in shell.


Update multiple documents:

> Db. users. update ({"sr": "10/13/1978" },{ $ set: {"gift": "happy" }}, false, true)


Return to the update document:

This article is from the "cooking wine and tea" blog and will not be reposted!

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.