mongoose-subpopulate
mongoose-subpopulate copied to clipboard
In the example, results are not as expected.
Hey there,
I am a big fan of this whole thing. It seems like a good idea and I can tell I'm almost getting what I expect but it isn't quite working. I used the example, but I removed some properties from the model (so now it's just username and best_friend) and then populated a database with three guys. The first one has no best friend, but the second has the first as a best friend, and the third has the second as a best friend.
I changed how the result is handled a little bit to make it nicer to read, like so:
var m = require('./models.js').createMongooseObject();
m.connect('mongodb://localhost/spandex-test', function () {
console.log('connected to database');
m.User.find()
.populate('best_friend')
.populate('best_friend.best_friend').exec(function (err, result) {
result.forEach(function (user) {
console.log(JSON.stringify(user, null, 4));
if (user.best_friend) {
console.log(user.username + '\'s BFF: ', result.best_friend.username);
if (user.best_friend.best_friend) {
console.log('-> ' + user.username + '\'s BFF\'s BFF: ', result.best_friend.best_friend.username);
}
}
});
});
});
And this is what comes out:
{
"mongooseLink": {
"_id": "511395420d8e590f6fb5a4e4",
"username": "Steve"
},
"populatedObjects": {}
}
{
"mongooseLink": {
"_id": "511395520d8e590f6fb5a4e5",
"username": "James Jameson"
},
"populatedObjects": {}
}
{
"mongooseLink": {
"_id": "511395710d8e590f6fb5a4e6",
"username": "Bobby Bobson"
},
"populatedObjects": {}
}
Do you know why this might be happening? The model doesn't appear to actually be the model I expect, more like some raw data.
models.js is exactly the same apart from the extra properties. Is it possible that the example is out of date?
Thanks - and great work, I know a lot of people are using subpopulate with success.
Hi Dan, could you provide me with an example that creates the three dudes? Another thing I'm interested in is what happens if you only grab one at a time; and if the first one having no best_friend is what clobbers the whole thing. That shouldn't be the case but it might! I would write the test myself but I'm about to head to bed. What would be awesome, actually, would be for you to submit failing unit tests so I can just fix those.
There might be a few dragons lurking here still. I'm waiting with great expectation for mongoose proper to make this obsolete :)
I'm actually having issues running expect.js tests on Windows, I'm afraid, so no unit tests :(
As for the data, though, I'm simply setting it up through the mongo CLI.
db.users.insert({ "username" : "Bobby Bobson", "best_friend" : ObjectId("Whatever the best friend's ID is"})
And so on.
You should definitely get expect.js running, that would be very helpful in tracking down issues like this.
I was able to write some tests and verify that subpopulation of the same type (recursive population, I suppose; populate('field1').populate('field1.field1')) doesn't work as expected, but populate('field1').populate('field1.field2') works as expected. Not sure when I'll be able to fix this as it's not really one of my use-cases.