How to Get the ID of the Last Inserted Document in a MongoDB Collection using Java
When a document is inserted into a MongoDB collection, it has a unique
"_id" field. If a value isn’t explicitly provided for
"_id" upon insertion, MongoDB will generate one. There are times when you need to retrieve the ID of the most recently inserted document in a collection; fortunately, it’s easy to accomplish this task in Java with just a few lines of code. In this tutorial, we’ll explain how to get the ID of the last inserted document in a MongoDB collection with the Java driver.
Let’s take a moment to review a few key system requirements before diving into the code. There are a few prerequisites that need to be met:
You’ll need to make sure that MongoDB and the MongoDB Java driver are installed and configured beforehand.
You’ll also need to ensure that the latest Java JDK has been installed and configured beforehand.
Finally, you’ll need to confirm that the MongoDB service is running.
NOTE: Throughout this tutorial, we’ll assume that the MongoDB version used is 4.0 and MongoDB Java Driver is 3.8.2.
Get access to the MongoDB Deployment
Now that we’ve gone over the key prerequisites for this project, we can turn our attention to the Java code. We’ll start off by instantiating a MongoClient object. We do this by specifying both the hostname and the port number of our local deployment:
Get access to the MongoDB Database
Next, we’ll need to access a MongoDB database, which we do by using the
getDatabase() method. In this example, we’re accessing a database named
Get the ID of the Last Document Inserted in a MongoDB Collection using sort() Method
At this point, we’re ready to try out the code which will return the ID of the last document that was inserted in a collection. We’ll accomplish this task by sorting the results of a query. The
sort() method will specify the order on how the query returns the matching document. In this case, we’ll sort the
"_id" field of the collection
"SOME_COLLECTION" in descending order, which will ensure that the first item returned in the results is the last MongoDB document inserted in the MongoDB collection:
There might be just one line of code in the example above, but there’s a lot going on inside of it. Let’s review what we just did here:
- First, we accessed a specific MongoDB collection using
getCollection(). In this case, we accessed
- We then used the
find()method to execute a query. We didn’t supply a query in this example, which means that
find()would theoretically return all documents in the collection.
- However, please note that we used the
limit()method and set it to 1, which means that
find()will return only one document.
- Since the documents were sorted in descending order of
"_id", that single document returned will be the most recently added one.
You can also run this command in the Mongo Shell using the code shown below:
You’ll receive the same result that was returned using the Java code.
When a new document is inserted into a MongoDB collection, chances are you won’t know its ID. If a value isn’t explicitly supplied for
"_id", MongoDB will generate one upon insertion. Fortunately, it’s easy to retrieve the ID of the last inserted document with just a few lines of Java code. With the step-by-step instructions included in this tutorial, you’ll be ready to get the ID of the last inserted document in a MongoDB collection with the Java driver.