填充mongoose
我需要一些帮助来填充mongoose的子文件,我在互联网上search了很多,但没有find一种方法来解决我的问题。
我有两个架构: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;
我试图填充像下面的数组servidores,在path文件夹,但是当我打印种子variables,数组返回空,并需要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); }
可以帮我find解决这个问题的方法。
TKS
尝试将_id的SchemaType更改为ObjectId。
_id : ObjectId
此外,您不需要显式声明_id字段。 它是为所有Mongoose模式自动创build的。
这里是解决你的问题的文档咨询[ https://github.com/LearnBoost/mongoose/wiki/3.6-Release-Notes#added-documentpopulatedpath%5D [1 ]
首先应用查找查询和应用填充像这个代码片段
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); } })
它是否返回没有人口的任何价值? 也许问题不在于填充?