我有三种模式:饲料,链接和来源。
每个提要都有一对多关联的链接。
Feed.js:
links: {
collection: 'link',
via: 'feed'
}链接模型连接到源模型,具有一对一的关联。
Link.js:
source: {
model: 'source'
}在源模型中也没有什么特别的东西。
当我执行下面的查询时,我无法获得在源模型中定义的链接的细节。
Feed.find({}).populate('links').exec(console.log);结果:
{
links:
[
{
title: 'News link',
url: 'http://href.com/sfs',
feed: 1,
source: 1,
id: 1,
createdAt: '2014-08-23T13:01:42.989Z',
updatedAt: '2014-08-23T13:01:42.989Z'
}
],
title: 'Feed Title',
photo: 'img/img.png',
yesNo: false,
id: 1,
createdAt: '2014-08-23T13:00:43.317Z',
updatedAt: '2014-08-23T13:00:43.317Z'
} 我想用源模型填充“链接”集合。可以用水路吗?还是我必须手动操作?
谢谢,
发布于 2014-08-26 05:08:46
水系人口目前只有一个层次;多级人口在路线图上。目前最好的方法是使用已填充的链接执行初始Feed查询,从Link记录中收集所有source外键,同时查找它们,然后进行映射。类似于:
async.auto({
// Get all feeds
feeds: function(cb) {
Feed.find({}).populate('links').exec(cb);
},
// Get all sources of feed links
sources: ['feeds', function(cb, results) {
var sourceIds = results.feeds.reduce(function(memo, feed) {
return memo.concat(_.pluck(feed.links, 'source'));
}, []);
Source.find(sourceIds).exec(cb);
}],
// Map source objects onto links
mapping: ['feeds', 'sources', function(cb, results) {
// Index sources by ID
var sources = _.indexBy(results.sources, 'id');
var feeds = results.feeds.map(function(feed) {
feed.links = feeds.links.map(function(link) {
link.source = sources[link.source];
return link;
});
return feed;
});
return cb(null, feeds);
}]
}, function done(err, results) {
// Handle err or do something with results.mapping
});https://stackoverflow.com/questions/25466790
复制相似问题