There is an analogous post for using mongodb scripting mongodb: how to get the last N records?
How to achieve the same goal with Java getting the last inserted collection document? Just in case, I am using the 3rd version of the mongodb Java driver, my mongodb dependencies in pom.xml are as follows:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<version>3.0.2</version>
</dependency>