1

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 => {...
}

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.