Python + MongoDB auto-increment key value implementation

Source: Internet
Author: User
The following is a simple implementation of Python + MongoDB auto-incrementing key values. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at it with Xiaobian. Background

I recently wrote a test toolbox with a bug record system. I encountered a problem because I used Django and MongoDB in the background, how to implement an auto-increment field.

It is very easy to implement traditional relational databases. you only need to set an auto-incrementing field directly. when inserting data, you don't have to worry about this key value. you just need to process the data yourself, the auto-increment function is automatically implemented, but non-relational databases do not seem to have this function (or I don't know ). Baidu later found that they all used MongoDB settings, not what I wanted.

Solution

If Baidu does not find a good idea, it can only be solved by itself. my idea is very simple and the field will not be auto-incrementing, so I will create an auto-incrementing solution by myself.

I found that the method modified in MongoDB has a $ inc method, which can realize auto-increment of the int type. It is very simple to create a collection by yourself, and there is only one int field in this collection. you can get the ID of this collection every time you insert data, then, call the $ inc method to implement the auto-increment scheme.

Code Display

Python is quite simple to implement. Python is very compatible with Django. The code is as follows:

Def bugPlus (self): "" bugID auto-increment: return: True "" db = self. _ chooseCollection (config. COLLECTION ['bucgid']) db. update_one ({"bugID": self. getBugID () },{ "$ inc": {"bugID": 1 }}) return True

After each successful insertion, you can call this method to implement ID auto-increment.

Def getBugID (self): "Get the latest bug No.: return: None" db = self. _ chooseCollection (config. COLLECTION ['bucgid']) rst = db. find_one () return rst ['bucgid']

Call this method before data insertion. the inserted ID is the auto-increment ID.

Disadvantages

Of course, this method still has some disadvantages. when calling the method, you need to confirm whether the method is successfully executed. Otherwise, the ID inserted next time will not be the auto-increment data. If you call a method multiple times, the performance may decrease.

Others

If you have a better implementation method, please let me know!

The simple implementation of the above Python + MongoDB auto-incrementing key value is all the content that I have shared with you. I hope to give you a reference and support for PHP.

For more articles on simple implementation of Python + MongoDB auto-incrementing key values, refer to PHP Chinese network!

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.