0

I have the following data in Robo3t Mongo collection With this model:

const eleccionSchema = new mongoose.Schema({
    e: [{
        id: {
            type: String,
            required: true
        },
        l:[...]
    }],
    eleccion: {
        type: Number,
        required: true,
        ref: 'Corte'
    }
})
//? Create the model
const Eleccion = mongoose.model('Eleccion', eleccionSchema)

Right now I'm trying to fetch some data based on e.id like this

const eleccion = await Eleccion.findOne({'e.id':'A'})

But it's actually returning the whole array instead of just one output

1 Answer 1

1

Fixed it with a projection: https://docs.mongodb.com/manual/reference/operator/projection/elemMatch/

const eleccion = await Eleccion.findOne({}, {
 'e':
   { $elemMatch: { id: 'A' } }
})
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.