Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
How do I find most recent (identical) document with findOne in Mongoose

I am trying to find a document in Mongoose by using findOne ( not find ), and I want to get the most recent document even though they may have identical fields.

For example, these are the documents in Mongoose:

// Let's say this one was created a week ago in Mongoose
{
  name: "John Smith",
  age: 27,
  industry: "finance"
}

// This one was created this morning
{
  name: "John Smith",
  age: 27,
  industry: "healthcare"
}

So I need to do a query along the lines of this:

const client = await Client.findOne({name: "John Smith"});

console.log(client);

/* 
result: 
{
  name: "John Smith",
  age: 27,
  industry: "finance"
} 

expected: 
{
  name: "John Smith",
  age: 27,
  industry: "healthcare"
}
*/

So when I Client.findOne by name, I'm expecting the most recent document. However Client.findOne seems to just return the old one.

Any advice?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

pass the argument as an Object sort: { 'created_at' : -1 } changing -1 to 1 will fetch the oldest record So your new query will look like

const client = Client.findOne({name: "John Smith"}).sort({created_at: -1});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!