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

77
Views
Mongo/Mongoose JS — Update document to latest state?

Is there a way to store a document then get an updated value of it at a later date without needing to query and populate again?

const someDocument = await SomeModel.findOne({...}).populate(...);

// store a reference to it for use
const props = {
   document: someDocument,
   ...
}

// at a later time
await props.document.getLatest() <-- update and populate in place?
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As describe here, You can define your own custom document instance methods by add functions on the schema level.

example taken from mongoose doc:

 // define a schema
 const animalSchema = new Schema({ name: String, type: String });

 // assign a function to the "methods" object of our animalSchema
 animalSchema.methods.findSimilarTypes = function(cb) {
   return mongoose.model('Animal').find({ type: this.type }, cb);
 };
 
 const Animal = mongoose.model('Animal', animalSchema);
 const dog = new Animal({ type: 'dog' });

 dog.findSimilarTypes((err, dogs) => {
   console.log(dogs); // woof
 });

This is the only way I can think of doing what you want - add a function that populate.

another thing that might help: you can create a pre hook on 'find' and add populate in it

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!