-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't make nested populate in new mongoose 3.6rc0 #1377
Comments
Works as designed. See the Model.populate() section in the pre-release notes |
Can you clarify how this is works as designed? I can't find anything in the docs referenced that shows me how I should approach this. I'm getting the same error. |
Can't make nested populate in new mongoose v3.6. Do I mistake it? output
package.json{
"name": "mongoose-populate-test",
"version": "0.1.0",
"private": true,
"dependencies": {
"mongoose": "3.6.0",
"async": "0.1.22"
},
"engines": {
"node": ">=0.8.21",
"npm": ">=1.2.11"
},
"scripts": {
"start": "node test.js"
}
} test.jsvar assert = require('assert');
var mongoose = require('mongoose');
var async = require('async');
var Schema = mongoose.Schema;
var ObjectId = mongoose.Types.ObjectId;
console.log('\n===========');
console.log(' mongoose version: %s', mongoose.version);
console.log('========\n\n');
var dbname = 'testing_populateAdInfinitum';
console.log('dbname: %s', dbname);
mongoose.connect('localhost', dbname);
mongoose.connection.on('error', function () {
console.error('connection error', arguments);
});
var phone = new Schema({
name: String
});
var Phone = mongoose.model('Phone', phone);
var user = new Schema({
name: String
, phone: { type: Schema.ObjectId, ref: 'Phone' }
});
var User = mongoose.model('User', user);
var blogpost = Schema({
title: String
, tags: [String]
, author: { type: Schema.ObjectId, ref: 'User' }
});
var BlogPost = mongoose.model('BlogPost', blogpost);
var createPhones = function(callback) {
var phoneIds = [new ObjectId, new ObjectId];
var phones = [];
phones.push({
_id: phoneIds[0]
, name: 'iPhone 5'
});
phones.push({
_id: phoneIds[1]
, name: 'GALAXY S'
});
Phone.create(phones, function(err, docs) {
assert.ifError(err);
callback(null, phoneIds);
});
};
var createUsers = function(phoneIds, callback) {
var userIds = [new ObjectId, new ObjectId];
var users = [];
users.push({
_id: userIds[0]
, name: 'mary'
, phone: phoneIds[0]
});
users.push({
_id: userIds[1]
, name: 'bob'
, phone: phoneIds[1]
});
User.create(users, function(err, docs) {
assert.ifError(err);
callback(null, userIds);
});
};
var createBlogPosts = function(userIds, callback) {
var blogposts = [];
blogposts.push({ title: 'blog 0', tags: ['fun', 'cool'], author: userIds[0] });
blogposts.push({ title: 'blog 1', tags: ['cool'], author: userIds[1] });
BlogPost.create(blogposts, function(err, docs) {
assert.ifError(err);
callback(null);
});
};
var findFunBlogPosts = function(callback) {
BlogPost.find({ tags: 'fun' }).lean().populate('author').exec(function (err, docs) {
assert.ifError(err);
var opts = {
path: 'author.phone'
, select: 'name'
};
BlogPost.populate(docs, opts, function(err, docs) {
assert.ifError(err);
docs.forEach(function(doc) {
console.log(doc);
});
callback(null);
});
});
};
mongoose.connection.on('open', function () {
async.waterfall([
createPhones,
createUsers,
createBlogPosts,
findFunBlogPosts
],
function(err, result) {
if (err) {
console.error(err.stack);
}
mongoose.connection.db.dropDatabase(function () {
mongoose.connection.close();
});
});
}); |
There is a context aware part of your code: |
I ran into the same problem. I think it has to do with the fact that even when the first For now, this is how I resolved it. I've made changes to your code as @kuba-kubula pointed out. And I extracted the Here's the code and the revisions view to see the changes I made. Hope this helps. |
@yangsu you are correct. Though there is another way. Either specify the model that should be used for population in the options to BlogPost.find({ tags: 'fun' }).lean().populate('author').exec(function (err, docs) {
assert.ifError(err);
User.populate(docs, {
path: 'author.phone',
select: 'name',
model: Phone // <== We are populating phones so we need to use the correct model, not User
}, callback);
// or use the model directly
Phone.populate(docs, {
path: 'author.phone',
select: 'name',
}, callback); This seems clunky. We should be able to do better with this in a future release. If the documents being populated are mongoose documents (opposed to plain objects) we could possibly use it's related schema to determine which Model to use. Won't work for plain objects of course. |
@kuba-kubula @yangsu @aheckmann |
BlogPost.find({ tags: 'fun' }).lean().populate('author').exec(function (err, docs) { User.populate(docs, { // or use the model directly Phone.populate(docs, { |
The reverse populate off of the model itself is pretty nice. Thanks for this. |
exports.padByID = function(req, res, next, id) {
}; This is simply not working for me :(. I cannot get populate() to work for me correctly, any help would be appreciated. The comments_pub Pad property is an array of generic objects, each of which has a User model associated to it. |
@aheckmann I've got the var User = mongoose.model('User');
var Grade = mongoose.model('Grade');
User.findOne({ ... })
.populate('array')
.exec(function (err, user) {
Grade.populate(user, {
path: 'array.grades',
select: 'name -_id'
}, function (error, updatedUser) {
//assuming no error,
user = updatedUser; //how do I persist / pass this on synchronously?
});
}); how do I get the user object returned from my |
@JaKXz what do you mean by persisting? You should be able to push/pull from the user.grades array. |
As the metadata for population is present in the ref property, shouldnt the libary be able to automatically guess the correct model for population? |
I don't understand your question, please clarify |
I think he's asking if the field you are trying to use to populate is correctly defined the Model schema. |
thanks you |
I have the following schemas:
I want to find a document with populated 'emails' and tags. When I try this:
I get the error:
The text was updated successfully, but these errors were encountered: