20

I am running a large data feed into Mongo that I need to have an external client connect to and tap into the feed from the last available record - not anything older than current. I have a tailable cursor that works just fine, but it starts at the beginning of the table, and I'd like to have it start at the most recently inserted record. I know how to do this, presuming I have the _ID of the last inserted record. My problem is that I can't get findOne working properly in Java to bring back the last inserted record. Put simply, I need the Java equivalent of this Mongo JS command:

db.market.findOne( {$query:{}, $orderby:{$natural:-1}} )

There are a couple of posts on here that I've found that seem similar, but they are assuming that the client is also the one inserting the records and already have knowledge of the last available ID.

Anyways, what would the proper corresponding Java code be to accomplish the same thing - that is getting the _ID of the last available record?

I guess as an alternative I could have my client insert a throwaway record, get that ID, and start from there, but I'd prefer to do this the right way.

Thanks

1
  • 3
    I've been looking for a full day and while there is basic documentation, I've found zero examples anywhere on the web, and I can't get any permutation I've tried to work. That is why I'm asking here. Commented Apr 9, 2014 at 14:56

5 Answers 5

55

To be clear, natural order is not insertion order, except in the case of capped collections. You will need another criteria to sort by.

Assuming you are using the default ObjectID, you can use this as a metric for insertion as the default value starts with the insertion time (to the millisecond) and is always unique.

You should also use a find, rather than a findOne. Try the following:

db.market.find({}).sort({_id:-1}).limit(1)
Sign up to request clarification or add additional context in comments.

3 Comments

Why .find rather than .findOne?
The findOne shell helper doesn't (or did not at time of writing) allow you to set sort options, etc.
The issue about findOne not working is no longer true.
2

if you want to do it in your JAVA code you cand do like this

Document myDoc = (Document)collection.find().sort(new BasicDBObject(<field>,-1)).first();

it will return a document the is the last inserted ordered by that significant field =)

Comments

2

For anyone who is looking for a more recent answer:

Here's how you finding the last inserted element by "_id" with mongodb-driver in your Java project.

// connect to your collection. 
MongoCollection<Document> collection = database.getCollection("yourCollection");

//request last inserted doc
Document myDoc = collection.find().sort(new Document("_id", -1)).first();

Hope this helps!

Comments

2

The best way:

.findOne({[WHERE]}, {sort: {_id: -1}, limit: 1 });

db.market.findOne({}, {sort: {_id: -1}, limit: 1 });

1 Comment

Minor correction, 2nd argument is for projection, the options object is in 3rd place. So it's db.market.findOne({}, {}, {sort: {_id: -1}, limit: 1 });
1
Document lastInsertion = new MongoClient().getDatabase("collectionName").find().sort(new BasicDBObject("_id", -1)).first();
lastInsertion.get("key");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.