node.js - 居住在 Mongoose 中

标签 node.js mongodb mongoose

我需要一些帮助来使用 Mongoose 填充子文档,我在互联网上搜索了很多,但没有找到解决我的问题的方法。

我有两个模式: 1 - 基础设施服务器

  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;

我正在尝试在路由文件夹中填充如下所示的数组 servidores,但是当我打印 seeds 变量时,数组返回空,并且需要 servidores.servidor (InfraServer 中的字段)、servidores._id 正确填充。

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

  }

可以帮我找到解决这个问题的方法。

谢谢

最佳答案

尝试将 _id 的 SchemaType 更改为 ObjectId。

_id : ObjectId

此外,您不需要显式声明 _id 字段。它是为所有 Mongoose 模式自动创建的。

关于node.js - 居住在 Mongoose 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27565290/

相关文章:

mongodb - 为什么查询扫描它找到的文档的 8 倍 - MongoDB

javascript - 获取类型错误 : Unknown encoding: 1 while running the server script with Mongoose

javascript - Mongoose find() 不适用于 $not 查询

node.js - 在 Mongoose 模式上保存数组属性

javascript - 使用 Mongoose 模式导入 CSV

node.js - 从 Node.js 服务器下载未命名文件时收到未找到错误

javascript - Nodejs创建一个里面有文字的PNG图像

javascript - 在 MongoDB/Mongoose 中聚合数组

mongodb - 如何在 mongoDB 中获取最后 50 个文档?

node.js - Laravel Elixir 的关键 CSS