简体   繁体   中英

Mongoose query MongoDB, find() object inside an object

Hello I have an Schema on Mongoose but have and object inside

const TrajesDeBano = new Schema({
    modelo:{type:String, required: true},
    tipo:{type:String, required:true},
    talla:[{
        s:{type:Number, required: true},
        m:{type:Number, required: true},
        l:{type:Number, required: true}
    }],
    precio:{type:Number, required: true}
});

when I do a callback to call "talla" returns an object

traje.findOne({'modelo':modelo}, function(err, trajeEncontrado){
      console.log(trajeEncontrado.talla)
});

[ {_id:5dd177918c7f021e4cab7e20, s: 15, m: 30, l: 25} ]

now I wanna call every "talla" singleone but a query trajeEncontrado.talla.s returns undefined . How can I find just the talla "S"?

Yes it will return undefined because of it don't know the array is, you could $unwind them or using $elemMatch

using $elemeMatch

Model.find({ modelo: 'Your Modelo', talla: { $elemMatch: { s: 2 } } })

See docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM