Settling in Mongoose

I need help to fill out a sub-document using mongoose, I searched a lot on the Internet but could not find a way to fix my problem.

I see two schemes: 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 try to fill in the servidores of the array, as shown below, in the routes folder, but when I print the seed variable, the array returns empty and I need servidores.servidor (field in InfraServer), servidores._id is filled 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); } 

May help me find a way to solve this problem.

Tks

+1
source share
4 answers

Try changing the SchemaType of the _id object to ObjectId.

 _id : ObjectId 

In addition, you do not need to explicitly declare the _id field. It is automatically created for all Mongoose schemas.

+3
source

First apply the search query, and the application fill in as 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); } }) 
+2
source

Does it return any values ​​without a population? Maybe the problem is not to populate?

+1
source

Source: https://habr.com/ru/post/956271/


All Articles