我需要一些帮助来使用 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/