填充()引用嵌套在对象数组中

我试图用Show模型中的数据填充()我的用户模型中的所有订阅。 我试过.populate('subscriptions.show'),但是对结果没有任何作用。

如果我订阅像这样的一个普通的Refs数组

subscriptions: [{type: Schema.Types.ObjectId, ref: 'Show'}] 

做一个填充('订阅')按预期工作

我已经看过在Stackoverflow上可以find的每个类似的问题,以及我可以在文档中find的东西。 我看不出我做错了什么。

完整的testing文件源,我正在与https://gist.github.com/anonymous/b7b6d6752aabdd1f9b59

架构和模型

 var userSchema = new Schema({ email: String, displayName: String, subscriptions: [{ show: {type: Schema.Types.ObjectId, ref: 'Show'}, favorite: {type: Boolean, default: false} }] }); var showSchema = new Schema({ title: String, overview: String, subscribers: [{type: Schema.Types.ObjectId, ref: 'User'}], episodes: [{ title: String, firstAired: Date }] }); var User = mongoose.model('User', userSchema); var Show = mongoose.model('Show', showSchema); 

初始数据

 var user = new User({ email: "test@test.com", displayName: "bill" }); user.save(function(err, user) { var show = new Show({ title: "Some Show", overview: "A show about some stuff." }); show.save(); user.subscriptions.push(show); user.save(); }); 

查询

 User.findOne({ displayName: 'bill' }) .populate('subscriptions.show') .exec(function(err, user) { if (err) { console.log(err); } console.log(user); }); 

结果是:

 { _id: 53a7a39d878a965c4de0b7f2, email: 'test@test.com', displayName: 'bill', __v: 1, subscriptions: [{ _id: 53a7a39d878a965c4de0b7f3, favorite: false }] } 

一些代码也许,还有一些更正你的方法。 你需要一个“manyToMany”types的连接,你可以做如下:

 var async = require("async"), mongoose = require("mongoose"), Schema = mongoose.Schema; mongoose.connect('mongodb://localhost/user'); var userSchema = new Schema({ email: String, displayName: String, subscriptions: [{ type: Schema.Types.ObjectId, ref: 'UserShow' }] }); userShows = new Schema({ show: { type: Schema.Types.ObjectId, Ref: 'Show' }, favorite: { type: Boolean, default: false } }); var showSchema = new Schema({ title: String, overview: String, subscribers: [{ type: Schema.Types.ObjectId, ref: 'User' }], episodes: [{ title: String, firstAired: Date }] }); var User = mongoose.model('User', userSchema); var Show = mongoose.model('Show', showSchema); var UserShow = mongoose.model('UserShow', userShows); var user = new User({ email: 'test@test.com', displayName: 'bill' }); user.save(function(err,user) { var show = new Show({ title: "Some Show", overview: "A show about some stuff." }); show.subscribers.push( user._id ); show.save(function(err,show) { var userShow = new UserShow({ show: show._id }); user.subscriptions.push( userShow._id ); userShow.save(function(err,userShow) { user.save(function(err,user) { console.log( "done" ); User.findOne({ displayName: "bill" }) .populate("subscriptions").exec(function(err,user) { async.forEach(user.subscriptions,function(subscription,callback) { Show.populate( subscription, { path: "show" }, function(err,output) { if (err) throw err; callback(); }); },function(err) { console.log( JSON.stringify( user, undefined, 4) ); }); }); }); }); }); }); 

这应该显示一个人口稠密的答复,就像这样:

 { "_id": "53a7b8e60462281231f2aa18", "email": "test@test.com", "displayName": "bill", "__v": 1, "subscriptions": [ { "_id": "53a7b8e60462281231f2aa1a", "show": { "_id": "53a7b8e60462281231f2aa19", "title": "Some Show", "overview": "A show about some stuff.", "__v": 0, "episodes": [], "subscribers": [ "53a7b8e60462281231f2aa18" ] }, "__v": 0, "favorite": false } ] } 

或者没有“manyToMany”作品。 请注意,没有初始调用来填充:

 var async = require("async"), mongoose = require("mongoose"), Schema = mongoose.Schema; mongoose.connect('mongodb://localhost/user'); var userSchema = new Schema({ email: String, displayName: String, subscriptions: [{ show: {type: Schema.Types.ObjectId, ref: 'UserShow' }, favorite: { type: Boolean, default: false } }] }); var showSchema = new Schema({ title: String, overview: String, subscribers: [{ type: Schema.Types.ObjectId, ref: 'User' }], episodes: [{ title: String, firstAired: Date }] }); var User = mongoose.model('User', userSchema); var Show = mongoose.model('Show', showSchema); var user = new User({ email: 'test@test.com', displayName: 'bill' }); user.save(function(err,user) { var show = new Show({ title: "Some Show", overview: "A show about some stuff." }); show.subscribers.push( user._id ); show.save(function(err,show) { user.subscriptions.push({ show: show._id }); user.save(function(err,user) { console.log( "done" ); User.findOne({ displayName: "bill" }).exec(function(err,user) { async.forEach(user.subscriptions,function(subscription,callback) { Show.populate( subscription, { path: "show" }, function(err,output) { if (err) throw err; callback(); }); },function(err) { console.log( JSON.stringify( user, undefined, 4) ); }); }); }); }); }); 

检查答案: https : //stackoverflow.com/a/28180427/3327857

  Car .find() .populate({ path: 'partIds', model: 'Part', populate: { path: 'otherIds', model: 'Other' } })