这是我的架构:
var sourcesSchema = {
title: String,
name: String,
url: String,
description: String,
category: Array,
rating: Number,
source_pages: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'source_page',
}]
}
var sourcePageschema = {
uname: String,
source_name: String,
page_address: String,
driver_name: String,
product: {
type: mongoose.Schema.Types.ObjectId,
ref: 'products' //Edit: I'd put the schema. Silly me.
}
}
var productsSchema = {
title: String,
uname: String,
descriptin: String,
images: Array,
currency: String,
last_update_time: Number,
last_process_time: Number,
meta_data: {},
tags: Array,
min_price: Number,
max_price: Number,
prices: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'prices' //Edit: I'd put the schema. Silly me.
}]
}
此代码可以成功运行并填充source_pages:
_sources.find().populate('source_pages').exec(function (err,sources) {
res.json(200, sources);
});
但如果我也想填充产品:
_sources.find().populate('source_pages').populate('source_pages.product').exec(function (err,sources) {
res.json(200, sources);
})
此错误:
TypeError:无法调用未定义的方法'path' 在搜索(/home/sina/rhino2/node_modules/mongoose/lib/model.js:2088:28) 在搜索(/home/sina/rhino2/node_modules/mongoose/lib/model.js:2107:22) 在Function._getSchema(/home/sina/rhino2/node_modules/mongoose/lib/model.js:2114:5) 在populate(/home/sina/rhino2/node_modules/mongoose/lib/model.js:1719:22) 在Function.Model.populate(/home/sina/rhino2/node_modules/mongoose/lib/model.js:1702:5) 在cb(/home/sina/rhino2/node_modules/mongoose/lib/query.js:1690:11) 在/home/sina/rhino2/node_modules/mongoose/lib/utils.js:414:16 at /home/sina/rhino2/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:158:16 at commandHandler(/home/sina/rhino2/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:643:16) 在null。 (/home/sina/rhino2/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1641:20)
答案 0 :(得分:2)
我正在寻找同样的问题,我相信你要找的是Mongoose: deep population (populate a populated field)。
基本上,除非您在回调函数中执行此操作,然后将其插入到返回中,否则您无法执行您要执行的操作。我试图避免这种情况,但此刻它似乎是唯一的选择。另一个选择,如果你打算做很多这类东西,那就是考虑使用一个关系数据库。