1

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

2 Answers 2

3

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.

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

1 Comment

Tks for help , now I have the return to 'servidores' populate. Tks for help
2

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);

      }

    })

1 Comment

Tks for help again , but didn't work too. I received the error : servidores: [ 544a246ca6492c492d000007 ] } has no method 'populate'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.