MongoDB self-Increasing ID implementation method _mongodb

Source: Internet
Author: User
Tags mongodb

The example of this article describes the implementation method of MongoDB ID. Share to everyone for your reference, specific as follows:

First create an automatic growth ID collection IDs

>db.ids.save ({name: "User", id:0});

You can see if it's successful.

> Db.ids.find ();
{"_id": ObjectId ("4c637dbd900f00000000686c"), "name": "User", "id": 0}

And then add the IDs collection to get the IDs each time before adding new users

Then, each time you add a new user to the Db.user collection, the ID value of the name= "user" document in the Db.ids collection is added 1 to the document

>userid = Db.ids. Findandmodify ({update:{$inc: {' id ': 1}}, query:{' name ': ' User '}, new:true});
{"_id": ObjectId ("4c637dbd900f00000000686c"), "name": "User", "id": 1}

Note: Because Findandmodify is a method that completes the update lookup for two operations, it is atomic and multithreading does not conflict.

And then save the appropriate data

>db.user. Save ({uid:userid.id, username: "Dotcoo", Password: "Dotcoo", Info: "http://www.jb51.net/"});
---------Update 2011-03-27 13:11------------------------

In fact, the top two lines can be written as one step

>db.user. Save ({
  uid:db.ids. findandmodify {
    update:{$inc: {' id ': 1}},
    query:{' name ': ' User '},
    New:true}). ID,//Let the ID value of the name= "user" document in the Db.ids collection add 1 and return as the ID
  username: "Dotcoo",
  Password: "Dotcoo",
  Info: "http://www.jb51.net/  "});
---------Update 2011-03-27 13:11------------------------

View Results

> Db.user.find ();
{"_id": ObjectId ("4c637f79900f00000000686d"), "UID": 1, "username": "admin", "password": "Admin"}

This is the shell of the MONGO, if you are using the server-side Java PHP Python, you can encapsulate these operations, using only a few parameters to return the ID, you can also implement a cross table like Oracle ID.

I wrote a section of PHP, take out to share.

<?php
function Mid ($name, $db) {
  $update = array (' $inc ' =>array ("id" =>1));
  $query = Array (' name ' => $name);
  $command = Array (
      ' findandmodify ' => ' IDs ', ' Update ' => $update,
      ' query ' => $query, ' new ' =>true, ' Upsert ' =>true
  );
  $id = $db->command ($command);
  return $id [' value '] [' id '];
}
$conn = new Mongo ();
$db = $conn->idtest;
$id = Mid (' user ', $db);
$db->user->save (Array (' UID ' => $id, ' username ' => ' kekeles ', ' Password ' => ' kekeles ', ' info ' => ') http:/ /www.jb51.net/'));
$conn->close ();
? >

Its implementation is mainly the use of MongoDB findandmodify command, as long as each MongoDB insert object before the creation of an ID assignment to the _id is OK, because its implementation to meet the atomicity, so there is no concurrency problem.

In addition, the findandmodify itself provides a upsert parameter, which is true to insert automatically, but that cannot customize the initial value, so the example in this article does not use Upsert.

BTW, the name of the database "_seq" begins with an underscore, so the list will be in front of you and easily distinguishable.

I hope this article will help you to MongoDB database program design.

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.