Mongoose 4.13 was released last week. As usual, its packed with powerful new features. The most exciting new feature is the ability to set localField , foreignField , and ref dynamically for virtual populate. The syntax for virtual populate's dynamic properties is slightly different than the one for conventional populate, but we think this new syntax is much more powerful.

Introducing Dynamic Virtual Populate Properties

Virtual populate creates a virtual property (one that is not stored in the database) that can look up documents from other collections. There are 4 options for configuring virtual populate, in addition to the name of the virtual: ref , localField , foreignField , and justOne .

const mongoose = require ( 'mongoose' ); mongoose.Promise = global.Promise; mongoose.connect( 'mongodb://localhost:27017/test' , { useMongoClient: true }); var PersonSchema = new mongoose.Schema({ name: String , band: String }); var BandSchema = new mongoose.Schema({ name: String }, { toObject: { virtuals: true } }); BandSchema.virtual( 'members' , { ref: 'Person' , localField: 'name' , foreignField: 'band' , justOne: false }); var Person = mongoose.model( 'Person' , PersonSchema); var Band = mongoose.model( 'Band' , BandSchema);

You can call .populate('members') to find documents in the 'Person' model's collection whose band property equals the band's name property. For example:

run().catch(error => console .error(error)); async function run ( ) { await mongoose.connection.dropDatabase(); await Person.create([ { name: 'Axl Rose' , band: 'Guns N\' Roses' }, { name: 'Slash' , band: 'Guns N\' Roses' }, { name: 'Vince Neil' , band: 'Motley Crue' }, { name: 'Nikki Sixx' , band: 'Motley Crue' } ]) await Band.create([ { name: 'Guns N\' Roses' }, { name: 'Motley Crue' } ]) const docs = await Band.find().sort({ name: 1 }).populate( 'members' ); console .log(docs[ 0 ]); console .log(docs[ 1 ]); }

Before mongoose 4.13, localField , foreignField , and ref had to be strings, so they couldn't change based on the document. In mongoose 4.13 you can still set these properties to strings, but you can also set them to functions that take the current document as a parameter. This means you can determine what localField , foreignField , and ref are depending on the doc being populated.

For example, lets say you had 2 models, 'Person' and 'Organization', and people and organizations can send messages to each other represented by a 'Message' model. Suppose you wanted from and to virtuals that you could populate to the correct sender and recipient for the message. Before mongoose 4.13, this would not be possible with virtual populate because people and organizations are in separate collections. However, now that ref can be dynamic, you can do this:

const mongoose = require ( 'mongoose' ); mongoose.Promise = global.Promise; mongoose.connect( 'mongodb://localhost:27017/test' , { useMongoClient: true }); var PersonSchema = new mongoose.Schema({ name: String }); var OrganizationSchema = new mongoose.Schema({ name: String }); var MessageSchema = new mongoose.Schema({ fromModel: String , fromId: mongoose.Schema.Types.ObjectId, toModel: String , toId: mongoose.Schema.Types.ObjectId }, { toObject: { virtuals: true } }); MessageSchema.virtual( 'from' , { ref: doc => doc.fromModel, localField: 'fromId' , foreignField: '_id' , justOne: true }); MessageSchema.virtual( 'to' , { ref: doc => doc.toModel, localField: 'toId' , foreignField: '_id' , justOne: true }); var Person = mongoose.model( 'Person' , PersonSchema); var Organization = mongoose.model( 'Organization' , OrganizationSchema); var Message = mongoose.model( 'Message' , MessageSchema);

Here's how using these models looks. Mongoose abstracts out the interaction with fromModel and toModel . So all you need to do is .populate('from to') and you get the correct organization or person, depending on the value of fromModel and toModel .

run().catch(error => console .error(error)); async function run ( ) { await mongoose.connection.dropDatabase(); const [alice, bob] = await Person.create([{ name: 'Alice' }, { name: 'Bob' }]); const organization = await Organization.create({ name: 'C Corp' }); const messages = await Message.create([ { fromModel: 'Person' , fromId: alice._id, toModel: 'Person' , toId: bob._id, body: 'Hi Bob' }, { fromModel: 'Organization' , fromId: organization._id, toModel: 'Person' , toId: bob._id, body: 'Alice has sent you a message, accept?' }, { fromModel: 'Person' , fromId: bob._id, toModel: 'Organization' , toId: organization._id, body: 'Yes' } ]); const docs = await Message.find().sort({ _id: 1 }).populate( 'from to' ); console .log(docs[ 0 ].from); console .log(docs[ 0 ].to); console .log(docs[ 1 ].from); console .log(docs[ 1 ].to); console .log(docs[ 2 ].from); console .log(docs[ 2 ].to); }

Dynamic localField and foreignField

Dynamic refs are supported in conventional populate as well as virtual populate, but the limitation of conventional populate was always the fact that the fields were fixed. Conventional populate is like virtual populate, except foreignField must always be _id . The primary reason for adding virtual populate was letting you specify localField and foreignField , and mongoose 4.13 brings that promise to fruition by making localField and foreignField fully dynamic on a per-document basis.

For example, let's say you had an 'Assignment' model that models assigning a task to either a 'User' or an 'Admin'. If an admin is assigned, that overwrites the user's assignment, regardless of whether a user is also assigned. Here's how you would create the assigned virtual:

const mongoose = require ( 'mongoose' ); mongoose.Promise = global.Promise; mongoose.connect( 'mongodb://localhost:27017/test' , { useMongoClient: true }); var PersonSchema = new mongoose.Schema({ name: String }); var AdminSchema = new mongoose.Schema({ name: String }); var AssignmentSchema = new mongoose.Schema({ userId: mongoose.Schema.Types.ObjectId, adminId: mongoose.Schema.Types.ObjectId }, { toObject: { virtuals: true } }); AssignmentSchema.virtual( 'assigned' , { ref: doc => doc.adminId ? 'Admin' : 'User' , localField: doc => doc.adminId ? 'adminId' : 'userId' , foreignField: '_id' , justOne: true }); var User = mongoose.model( 'User' , UserSchema); var Admin = mongoose.model( 'Admin' , AdminSchema); var Assignment = mongoose.model( 'Assignment' , AssignmentSchema);

Here's an example of these models in action:

run().catch(error => console .error(error)); async function run ( ) { await mongoose.connection.dropDatabase(); const user = await User.create({ name: 'Homer Simpson' }); const admin = await Admin.create({ name: 'Mr. Burns' }); await Assignment.create([ { userId: user._id }, { userId: user._id, adminId: admin._id } ]); const docs = await Assignment.find().sort({ _id: 1 }).populate( 'assigned' ); console .log(docs.map(doc => doc.assigned)); }

Moving On