MangoDB creates, updates, and deletes documents

Source: Internet
Author: User
Tags php database

MangoDB creates, updates, and deletes documents

Preface

The previous article has a general understanding of the basic operations of MongoDB. In this article, I will explain in detail how to create, update, and delete MangoDB.

Related reading: and

1. Insert and save the document

Insert is the most basic method in MongoDB. insert data into a collection as described in the previous article:

[Javascript]
  1. Microsoft Windows XP [version 5.1.2600]
  2. (C) Copyright 1985-2001 Microsoft Corp.
  3. C: \ Documents ents and Settings \ Administrator> E:
  4. E: \> cd MongoDB
  5. E: \ MongoDB> cmd.exe
  6. MongoDB shell version: 2.0.2
  7. Connecting to: test
  8. > Use php// Select the php Database
  9. Switched to db php
  10. > Show collections// View the set in the phps database.
  11. Blog
  12. System. indexes
  13. > Db. blog. insert ({"Title":"Okokok","Content":"123456","Time":"2011023023","Comments":"23224"})// Select the blog set. And insert data.
  14. > Db. blog. find ()// View all the documents in the blog set, which have been inserted successfully.
  15. {"_ Id": ObjectId ("4f0bc1245a5904bae2b767a8"),"Title":"Wowowowo","Content"
  16. :"Hahahahah","Time": ISODate ("2012-01-10T03: 38: 45.515Z")}
  17. {"_ Id": ObjectId ("4f0bc11d5a5904bae2b767a7"),"Title":"Wowowowo","Content"
  18. :"Hahahahah","Time": ISODate ("2012-01-10T03: 38: 45.515Z"),"Comments": "Hao
  19. Hao "}
  20. {"_ Id": ObjectId ("4f0cf96bbc8c53013bb90682"),"Title":"Okokok","Content":
  21. "123456","Time":"2011023023","Comments":"23224"}
  22. >

In the above document, there is a "_ id", which is intended to be explained in the second article.

A unique document identification is required within a specific set. Therefore, all documents stored in MongoDB are completed by a "_ id" key. The value of this key can be of any type. The ObjectId object is used by default. The idea of generating ObjectId objects is the topic of this article and can be used for reference by many distributed systems.

In order to consider the distribution, "_ id" requires different machines to generate it conveniently using the globally unique method of the same type. Therefore, the auto-increment primary key cannot be used (it is time-consuming and laborious to synchronize multiple servers). Therefore, the ObjectId object generation method is used.

ObjectId uses a 12-byte storage space, which is generated as follows:

| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |

| Timestamp | machine ID | PID | counter |

The first four bytes of Timestamp are timestamps starting from the standard epoch, in seconds. They have the following features:

  1. The timestamp is the same as the last five bytes to ensure the uniqueness in seconds;
  2. Ensure that the insertion order is roughly sorted by time;
  3. Indicates the document creation time;

The actual timestamp value is not important. You do not need to synchronize the time between servers (because the machine ID and process ID are added to ensure that the value is unique, and the uniqueness is the final appeal of ObjectId ).

The machine ID is the server host ID, usually the hash value of the machine host name.

Multiple mongod instances can run on the same machine. Therefore, you also need to add the process identifier PID.

The first nine bytes ensure the uniqueness of ObjectId generated by different processes on different machines in the same second. The last three bytes are an automatically added counter (A mongod process requires a global Counter), ensuring that the ObjectId in the same second is unique. Each process can have up to 256 ^ 3 = 16777216 ObjectId in a second.

To sum up, the timestamp must be unique in seconds. The machine ID must be distributed during design to avoid clock synchronization. The PID ensures the uniqueness when multiple mongod instances run on the same server, the last counter ensures the uniqueness within the same second (several bytes should be selected to consider both the economics of storage and the maximum concurrency performance ). "_ Id" can be generated either on the server or on the client, which can reduce the pressure on the server.

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.