Simple implementation of PYTHON+MONGODB self-increment key value

Source: Internet
Author: User
background

I recently wrote a Test kit with a bug recording system, because I implemented it with Django and MongoDB in the background, and encountered a problem with how to implement a self-increment field.

The traditional relational database is very easy to implement, as long as the direct setting of a self-increment field on the line, the insertion of data without the value of the key, just to handle the data on the line, will automatically implement the self-increment function, but the non-relational database does not seem to have this function (or I do not know). After Baidu found all is MongoDB's setup method, not I want.

Solution Ideas

Baidu did not find a good idea, it can only solve their own, my idea is very simple, the field will not be self-increasing, then build a self-increment program.

I found that there is a $inc way to modify the method in MongoDB. You can implement an int type's self-increment. Then it is very simple, build a collection, and then this collection only an int field, each time you insert data to the collection to take the ID, and then call $inc method, then the implementation of the automatic self-increment scheme.

Code Show

Python is very simple to implement, and Python and Django fit very well. The code is as follows:

def bugplus (self): "" "  bugid increment  : Return:true" ""  db = self.__choosecollection (config. collection[' BugID ')  db.update_one ({"BugID": Self.getbugid ()}, {"$inc": {"BugID": 1}})  return True

After each insert succeeds, this method can be used to implement the self-increment of the ID.

def getbugid (self): ""  Gets the latest number of the current bug  : Return:none  "" "  db = self.__choosecollection (config. collection[' BugID ')  rst = Db.find_one ()  return rst[' BugID ']

This method is called before inserting, so that the ID that is inserted each time the data is inserted is the implementation of the self-increment ID.

Disadvantages

Of course, this approach still has shortcomings, the invocation of the time to use the method, so you need to confirm that the method is successful, otherwise it will cause the next insert ID is not the data after the increment. A multi-tone method at a time can cause performance degradation.

Other

If there is a better way to achieve, please tell me!

The above Python+mongodb self-increment key value of the simple implementation is the small part to share all the content of everyone, I hope to give you a reference, but also hope that we support topic.alibabacloud.com.

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.