User_id autoincremet in mongodb using python

I want to autoincrement user_id by 1 in python

Hi @sarthak_3222,

First of all great question :slight_smile:

You can use PyMongo for this. It is a Python distribution containing tools for working with MongoDB, and is the recommended way to work with MongoDB from Python.

Here is the code for achieving the same.

import pymongo as pm
client = pm.MongoClient('localhost', 27017)

# The above line will connect us to the specified host and port

db = client.test_databases

# Accessing the test_databases using the db variable.

posts = db.posts

# Creating the posts object to insert the documents into the 'posts' collection.

for x in range(1,10): 
   posts.insert_one({ "_id" : x, "name" : "Shubham", "role" : 'CSE' })

Now, this is what the result looks like :

if you have any other issue, feel free to get back to us. Happy Learning :slight_smile:

Thanks,
Shubham Ranjan
Curriculum Support Engineer

I want to increment user_id at time of
inserting the data

Hi @sarthak_3222,

You can easily achieve the same by maintaining a seperate variable in your python environment and every time you insert a document into your collection, just increment it by one.

I am going to demonstrate the same using a for loop. You can modify the code according to your needs to achieve the same.

import pymongo as pm

client = pm.MongoClient('localhost', 27017)
db = client.test_databases
posts = db.posts

# Creating a variable to store the value of user_id
user_id = 100

for x in range(1,10):
    # incrementing the user_id before inserting the document.
    user_id+=1
    posts.insert_one({"user_id":user_id,"name":"Shubham", "role":'CSE'})

Output

if you have any other issue, feel free to get back to us. Happy Learning :slight_smile:

Thanks,
Shubham Ranjan
Curriculum Support Engineer

Thank you so much sir for your reply now I can easily modify my code as per my requirements.
Thank so much for helping me.

Hey @sarthak_3222 , I’m curious as to your solution. Are you performing a filter or something to find your max _id and then adding 1? Or are you doing it some other way?

@Shubham_13709, you mention that PyMongo is recommended. Can PyMongo be used with Django? Do you have any best practices or recommendations for using MongoDB with Django?

No sir i don’t have any recommendations for using django even I’m beginner in mongodb and in couple of days I will start learning django to make website

Hi @paragonss,

Please refer this great article on using Django with MongoDB. Do let me know if you find it useful or not.

If you have any other issue, feel free to get back to us. Happy Learning :slight_smile:

Thanks,
Shubham Ranjan
Curriculum Support Engineer

1 Like

Sir the code that you gave me
It inserts the same record 10 times and I have to insert that record only one in the document what should I do? Now

Hi @sarthak_3222,

As I said earlier, you can modify this code to suit your needs. I just took a random use case in the above posts. Can you please share some more details about your use case such as what kind of documents you are having and how are you generating them etc.

Thanks,
Shubham Ranjan
Curriculum Support Engineer

Well I make another document to store previously added data in employee document and another document I will store the id of currently added data and based on that data I want to increment the user_id.

Thanks @Shubham_13709! have you had any experience using Djongo? it seems to be based on the existing Django models, etc. I’m trying to determine which I will prefer.