1

I can't find the answer. I have a query that can return many answer and I want to get the last one. How to do it?

db.users.find({username: "bob", sort: {createdAt: -1}}).first()

I would like something like this.

1
  • The reply and "duplicate" are what you're asking, right? You can do this: db.collection.find({ username: "bob" }).sort({ createdAt: -1 }).limit(1).toArray()[0] Commented Jul 11, 2024 at 10:18

1 Answer 1

0

do this to get last N records :

function last(N) {
    return db.collection.find().skip(db.collection.count() - N);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.