MongoDB field Growth

Source: Internet
Author: User

The document stored in MongoDB must have a "_id" key. The value of this key can be any type, and the default is a Objectid object.

ObjectId is a 12-byte BSON type data that has the following format:

    • The first 4 bytes represent a timestamp
    • The next 3 bytes are the machine identification code
    • The immediate two bytes consist of a process ID (PID)
    • The last three bytes are a random number.

Within a collection, each collection has a unique "_id" value to ensure that each document within the collection is uniquely identified.

MongoDB uses Objectid, not the main reason for other, more conventional practices, such as auto-incremented primary keys, because it is laborious and time consuming to automatically increase primary key values on multiple servers.

While MongoDB does not have auto-growing capabilities like SQL, in some cases we may need to implement OBJECTID auto-grow.

Since MongoDB does not implement this function, we can do it programmatically, the following we will implement the _id field automatically grow in the Counters collection.

Using the Counters Collection

Consider the following products documentation. We want the _id field to implement the auto-grow function from 1,2,3,4 to N. To do this, create a counters collection where the sequence field values can be automatically long

>db.createcollection ("counters")

The Sequence_value field is a value after the sequence is automatically grown.

Use the following command to insert the counters collection in the sequence document:

>db.counters. Insert ({_id: "ProductID", Sequence_value:0})
Creating Javascript Functions

Now, we create the function Getnextsequencevalue as input to the sequence name, and the specified sequence automatically grows by 1 and returns the latest sequence value. In the example in this article, the sequence is named ProductID.

> function Getnextsequencevalue (sequencename) {   var=  db.counters.findAndModify (      {         Query:{_id:sequencename},          Update: {$inc: {sequence_value:1}},         new:true      });    return Sequencedocument.sequence_value;}
Using Javascript functions

Next we will use the Getnextsequencevalue function to create a new document and set the document _ID automatically for the returned sequence value:

>db.products. Insert ({   "_id": Getnextsequencevalue ("ProductID"),   "Product_Name": "Apple iPhone",   "category": "Mobiles" })>db.products. Insert ({   "_id": Getnextsequencevalue ("ProductID"),   "Product_Name": "Samsung S3",   "category": "Mobiles" })

As you can see, we use the Getnextsequencevalue function to set the _id field.

To verify that the function is valid, we can read the document using the following command:

>db.prodcuts.find ()

The above command will return the following results, and we find that the _id field is self-growing:

12, "product_name": "Samsung S3", "category": "Mobiles"}

ObjectId is a 12-byte BSON type data that has the following format:

  • The first 4 bytes represent a timestamp
  • The next 3 bytes are the machine identification code
  • The immediate two bytes consist of a process ID (PID)
  • The last three bytes are a random number.

The document stored in MongoDB must have a "_id" key. The value of this key can be any type, and the default is a Objectid object.

Within a collection, each collection has a unique "_id" value to ensure that each document within the collection is uniquely identified.

MongoDB uses Objectid, not the main reason for other, more conventional practices, such as auto-incremented primary keys, because it is laborious and time consuming to automatically increase primary key values on multiple servers.

MongoDB field Growth

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.