How to get id from mongodb after insert

In this MongoDB tutorial, we are going to learn “How to get id from MongoDB after insert” with a few examples. We will cover this by executing different operations with examples. The following list topics we are going to cover in this tutorial:

  • How to get id from mongodb after insert
  • How to get the objectid in mongodb after an insert
  • How to get id from mongodb after an insert using python

How to get id from mongodb after insert

In this topic, you will learn to get the id of the document from MongoDB after inserting the documents into the collection.

Now, the below example will help you to understand to get id from MongoDB after inserting the document.

Example:

The following documents will insert into the getId collection.

db.getId.insertMany([
{ "_id" : "101", "name" : "James", "age" : "26", "city" : "USA" },
{ "_id" : "102", "name" : "Noah", "age" : "22" },
{ "_id" : "103", "name" : "Robert", "age" : "28", "city" : "United States" },
{ "_id" : "104", "name" : "Peter", "age" : "23", "city" : "Canada" },
{ "_id" : "105", "name" : "Jack", "age" : "25" }
])

We will use the find() method to return the document.

db.getId.find()
How to get id from mongodb after insert
How to get id from MongoDB after insert

We have successfully inserted the documents into the getId collection, Now, we will apply the following block of code to get the id from the MongoDB document.

# store the document into userId variable
var userId = db.getId.findOne({name: "Noah"})

# access the id of the document
userId._id

Here, we used the findOne() method to access a document where the name is Noah and store it into the userId variable. After that, access the id of the document using the variable.

Output:

How to get id from mongodb after inserting document
How to get id from MongoDB after inserting the documents

Read: MongoDB check if the document exists

How to get the objectid in mongodb after an insert

In this topic, you will learn to get the objectid of the document from MongoDB after inserting the documents into the collection.

An ObjectID is a 12-byte Field Of BSON type.

  • The first 4 bytes represent the Unix Timestamp of the document.
  • And, the next 3 bytes are the machine Id on which the MongoDB server is running,
  • After that, the next 2 bytes are of process id.
  • The last Field is 3 bytes used to increment the objected.

Now, the below example will help you to understand to get id from MongoDB after inserting the document.

Example:

The following documents will insert into the students’ collection.

db.students.insertMany([
{ "fname" : "Tom", "city" : "USA", "courses" : [ "c#", "asp", "node" ] },
{ "fname" : "Harry", "city" : "NY", "courses" : [ "python", "asp", "node" ] },
{ "fname" : "Mikky", "city" : "LA", "courses" : [ "python", "asp", "c++" ] },
{ "fname" : "Ron", "city" : "Dubai", "courses" : [ "python", "django", "node" ] }
])

We will use the find() method to return the document.

db.students.find()
How to get the objectid in mongodb after an insert
How to get the objectid in MongoDB after an insert

We have successfully inserted the documents into the students’ collection, Now, we will apply the following block of code to get the id from the MongoDB document.

# store the document into userId variable
var userId = db.students.findOne({ fname: "Mikky"})

# access the id of the document
userId._id

Here, we used the findOne() method to access a document where the fname is Mikky and store it into the userId variable. After that, access the id of the document using the variable.

Output:

How to get the objectid in mongodb after inserting the documents
How to get the objectid in MongoDB after inserting the documents

Read: How to delete documents in MongoDB

How to get id from mongodb after an insert using python

In this topic, you will learn to get the id of the document from MongoDB after inserting the documents into the collection using python.

Now, the below example will help you to understand to get id from MongoDB after inserting the document using python.

Example:

The following block of python code is used to return the id field of the MongoDB document.

  • First, we have imported the pymongo library to working with the MongoDB.
  • After that, we make the connection with the host.
  • And, we have accessed the existing database and collection, mydatabase and customers respectively.
  • After that, insert a document into the collection.
  • And, use the inserted_id property that holds the id of the inserted document.
  • Use the print() function to display the id of document.
import pymongo

myclient = pymongo.MongoClient("mongodb://127.0.0.1:27017")
mydb = myclient['mydatabase']
mycol = mydb["customers"]

mydict = { "name": "Jack", "address": "Lowstreet 27, USA" }

x = mycol.insert_one(mydict)

print(x.inserted_id)

Output:

How to get id from mongodb after an insert using python
How to get id from MongoDB after an insert using python

We have successfully returned the id of the MongoDB document using python.

You may also like to read the following articles on MongoDB.

In this tutorial, we have learned about getting the id from MongoDB after inserting. And, Here is the complete list of topics that we have discussed.

  • How to get id from mongodb after insert
  • How to get the objectid in mongodb after an insert
  • How to get id from mongodb after an insert using python