LearnBoost/mongoose — pull request #1879 by mihai-chiorean (2014-01-16 01:13:26+00:00)
Title: Populate can use a Model as a parameter

Use a model with .populate() to populate from other DBs / connections

// create first connection+db (mongoose_test)
var db = start()

// create second connection+db (mongoose_test2)
, db2 = mongoose.createConnection('mongodb://localhost/mongoose_test2')

// this model is on the first db
, BlogPost = db.model('RefBlogPost', posts + '1')

// this model is on the second db
, User = db2.model('RefUser', users + '1');

User.create({
name: 'Guillermo'
, email: 'rauchg@gmail.com'
}, function (err, creator) {
console.dir(creator);
assert.ifError(err);

BlogPost.create({
title : 'woot'
, _creator : creator._id
}, function (err, post) {
assert.ifError(err);
/**
* populate takes as a 3rd parameter the User model (second db) and uses it ti query for the population data
*/
BlogPost
.findById(post._id)
.populate('_creator', 'name', User)
.exec(function (err, post) {
db.close();
db2.close();
assert.ifError(err);
assert.ok(post._creator.name == 'Guillermo');
done()
});
});
})