node.js - Populate() ref nested in object array -



node.js - Populate() ref nested in object array -

i trying populate() subscriptions in user model info show model. have tried .populate('subscriptions.show') nil results.

if create subscriptions plain array of refs so

subscriptions: [{type: schema.types.objectid, ref: 'show'}]

doing populate('subscriptions') works intended

i have looked @ every similar question find on stackoverflow , find on docs. can't see doing wrong.

complete test file source working https://gist.github.com/anonymous/b7b6d6752aabdd1f9b59

schema , models

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);

initial data

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 stuff." }); show.save(); user.subscriptions.push(show); user.save(); });

the query

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

results in:

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

some code perhaps, corrections approach. kind of want "manytomany" type of bring together can create follows:

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 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) ); }); }); }); }); }); });

that should show populated response much this:

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

or without "manytomany" works also. note here there no initial phone call populate:

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 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) ); }); }); }); }); });

node.js mongodb mongoose

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -