Design and Implementation of MongoDB auto-incrementing primary key id Generator

Source: Internet
Author: User

Design and Implementation of mongodb auto-incrementing primary key id Generator

 

Article: http://blog.csdn.net/5iasp/article/details/8794396

Author: javaboy2012
Email: yanek@163.com
Qq: 1046011462

 

Scenario: assume there are two systems:

System A and system B

Use different programming languages to operate a mongodb table, such as the news table.

Related fields include

News_id

Name

Title

Desc

Requirements:

News_id: The generated field is an integer int.

This id can be used to distinguish the category of news from the last digit. For example, if the end is 1, the end of domestic news is 2, indicating international news.

Below are the design ideas

Initialize the database:

Db. ids. save ({name: "china_news", id: 1 });
Db. ids. save ({name: "international_news", id: 2 });

Obtain the domestic news id: execute the following:

Db. ids. findAndModify ({update: {$ inc: {'id': 10 }}, query: {"name": "china_news"}, new: true });

Take the international news id: execute the following:

Db. ids. findAndModify ({update: {$ inc: {'id': 10 }}, query: {"name": "international_news"}, new: true });

Domestic News id: format:

1 11 21 31 41 51 61 xx1

International News id: The format is as follows:

1 22 22 32 42 52 62 xx2

 

Java code implementation:

Mainly rely on: findAndModify (queryObj, update); method to realize auto-increment,

Package com. test;

Import java.net. UnknownHostException;

Import com. mongodb. BasicDBObject;
Import com. mongodb. DB;
Import com. mongodb. DBCollection;
Import com. mongodb. DBObject;
Import com. mongodb. Mongo;
Import com. mongodb. parse exception;

Public class Test {
 
Public static String MONGODB_DATABASE_NAME = "test"; // mongodb Database Name
Public static String MONGODB_SERVER_IP = "192.168.0.1"; // ip address of the mongodb Database Server
Public static int MONGODB_SERVER_PORT = 17017; // mongodb database server port
Public static String MONGODB_TABLE_NAME_IDS = "ids"; // ids table

/**
* @ Param args
*/
Public static void main (String [] args ){

For (int I = 0; I <10; I ++)
{
System. out. println (getID ());

}

}
 
Public static String getID ()
{
String ret = "";
Mongo mongo = null;
Try {
Mongo = new Mongo (MONGODB_SERVER_IP, MONGODB_SERVER_PORT );
DB db = mongo. getDB (MONGODB_DATABASE_NAME );
Db. slaveOk ();
DBCollection topic = db. getCollection (MONGODB_TABLE_NAME_IDS );
BasicDBObject queryObj = new BasicDBObject ();
QueryObj. put ("name", "china_news ");
BasicDBObject update = new BasicDBObject ("$ inc", new BasicDBObject ("id", 10 ));
DBObject d = topic. findAndModify (queryObj, update );
// System. out. println (d. get ("id "));
Ret = d. get ("id"). toString ();

} Catch (UnknownHostException e ){
E. printStackTrace ();
} Catch (except exception e ){
E. printStackTrace ();
} Finally {
Mongo. close ();

}
Return ret;

}

}

Code output:

1
11
21
31
41
51
61
71
81
91

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.