I need some help to populate on sub-document with mongoose, I searched a lot in the internet , but didn't find a way to fix my problem.
I heve two schema : 1 - InfraServer
var InfraServerSchema = new Schema({
_id : String,
equipe : String,
servidor : String,
memoria : String,
processador : String,
modelo : String,
so : String,
usuario : String
},{ collection: 'infraserver' });
var InfraServer = mongoose.model('InfraServer', InfraServerSchema);
module.exports = InfraServer;
2 - InfraDataBase
var InfraDataBaseSchema = new Schema({
_id : String,
equipe : String,
nome_base : String,
vipname : String,
tipo_banco : String,
versao: String,
servidores : [{ type : mongoose.Schema.Types.ObjectId, ref: 'InfraServer' }],
tnsnames : String
},{ collection: 'infradatabase' });
var InfraDataBase = mongoose.model('InfraDataBase', InfraDataBaseSchema);
module.exports = InfraDataBase;
I'm trying to populate the array servidores like below, in routes folder, but when I print seeds variable, the array return empty, and need the servidores.servidor (field in InfraServer), servidores._id are populate correctly.
InfraDataBase.find().where('equipe').in(req.session.userInf.equipe).populate('servidores').exec(function(err, seeds){
if( err || !seeds) console.log("No seeds found");
else
{
console.log("--->>> " + seeds);
}
Can help me find a way to resolve this problem .
Tks
Try changing the SchemaType of the _id to ObjectId.
_id : ObjectId
Besides, you don't need to explicitly declare the _id field. It is automatically created for all Mongoose schemas.
First apply find query and the apply populate like this code snippet
InfraDataBase.find({equipe:{$in:[req.session.userInf.equipe]}},function(err,docs){
docs.populate('servidores').exec(function(err, seeds){
if( err || !seeds) console.log("No seeds found");
else
{
console.log("--->>> " + seeds);
}
})
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.