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

63
Views
Get only one element from nested array Mongoose

this is my schema

const ElectionScheme= new mongoose.Schema({
id:{
    type:Number
},
description:{
    type:String
},
startDate:{
    type:Date
},
closeDate:{
    type:Date
},
certificatesMaxAmount:{
    type:Number
},
voters:[{
   idCard:Number,
   credential:String,
   names: String,
   surnames:String,
   gender:String,
   birthDate:Date,
   department:String,
   idCircuit:Number,
   phone:Number,
   mail:String,
   votesCount:Number,
   certificatesCount:Number
}]

})

Hello, I am trying to extract a single voter by filtering by their idcard from an array in a model, but I always get the complete list of voters. I try this

const Elections = require("../models/Elections");
Elections.findOne({ id: voteData.idElection,"voters.idCard":voteData.idCard, "candidates.idCard": voteData.idCandidate})
.select(["candidates", "voters", "electionMode"])
.lean().then(election => {...
}

enter code here

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

0

I can't tell what's going on with the "candidates", but here's one way to filter/project what's returned from the "voters" array.

db.collection.find({
  "id": 3423423,        // <-- voteData.idElection
  "voters.idCard": 345  // <-- voteData.idCard
},
{
  "voters.$": 1
})

Try it with made up data on mongoplayground.net.

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!