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

128
Views
find from last element's(object) field in array of objects in mongoose

How to find data conditionally with mongoose by checking state field last element of history array.

{
        "_id" : ObjectId("5ff4360b3a1119002a6b8e04"),
        "name" : "sample"
        "history" : [ 
            {
                "_id" : ObjectId("56b0402a92b2a18116bf51de"),
                "state" : "pending"
            }, 
            {
                "_id" : ObjectId("56b0402a92b2a18116bf51fn"),
                "state" : "completed"
            }
        ],

    }

I try with following,

collection.find({$expr: {$eq: [{"$arrayElemAt": ["stateHistory", -1]}, "completed"]}})

Is there way to do this using find function without using aggregation?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

you can use $where

collection.find({
    $where: function(){
        return this.history[this.history.length-1].state === "completed"
    }
})
over 4 years ago · Santiago Trujillo 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!