Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

67
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda