Populate

MongoDB has the join-like $lookup aggregation operator in versions >= 3.2. Mongoose has a more powerful alternative called populate(), which lets you reference documents in other collections.

Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s). We may populate a single document, multiple documents, a plain object, multiple plain objects, or all objects returned from a query. Let’s look at some examples.

  1. const mongoose = require('mongoose');
  2. const Schema = mongoose.Schema;
  3. const personSchema = Schema({
  4. _id: Schema.Types.ObjectId,
  5. name: String,
  6. age: Number,
  7. stories: [{ type: Schema.Types.ObjectId, ref: 'Story' }]
  8. });
  9. const storySchema = Schema({
  10. author: { type: Schema.Types.ObjectId, ref: 'Person' },
  11. title: String,
  12. fans: [{ type: Schema.Types.ObjectId, ref: 'Person' }]
  13. });
  14. const Story = mongoose.model('Story', storySchema);
  15. const Person = mongoose.model('Person', personSchema);

So far we’ve created two Models. Our Person model has its stories field set to an array of ObjectIds. The ref option is what tells Mongoose which model to use during population, in our case the Story model. All _ids we store here must be document _ids from the Story model.

Note: ObjectId, Number, String, and Buffer are valid for use as refs. However, you should use ObjectId unless you are an advanced user and have a good reason for doing so.

Saving refs

Saving refs to other documents works the same way you normally save properties, just assign the _id value:

  1. const author = new Person({
  2. _id: new mongoose.Types.ObjectId(),
  3. name: 'Ian Fleming',
  4. age: 50
  5. });
  6. author.save(function (err) {
  7. if (err) return handleError(err);
  8. const story1 = new Story({
  9. title: 'Casino Royale',
  10. author: author._id // assign the _id from the person
  11. });
  12. story1.save(function (err) {
  13. if (err) return handleError(err);
  14. // that's it!
  15. });
  16. });

Population

So far we haven’t done anything much different. We’ve merely created a Person and a Story. Now let’s take a look at populating our story’s author using the query builder:

  1. Story.
  2. findOne({ title: 'Casino Royale' }).
  3. populate('author').
  4. exec(function (err, story) {
  5. if (err) return handleError(err);
  6. console.log('The author is %s', story.author.name);
  7. // prints "The author is Ian Fleming"
  8. });

Populated paths are no longer set to their original _id , their value is replaced with the mongoose document returned from the database by performing a separate query before returning the results.

Arrays of refs work the same way. Just call the populate method on the query and an array of documents will be returned in place of the original _ids.

Setting Populated Fields

You can manually populate a property by setting it to a document. The document must be an instance of the model your ref property refers to.

  1. Story.findOne({ title: 'Casino Royale' }, function(error, story) {
  2. if (error) {
  3. return handleError(error);
  4. }
  5. story.author = author;
  6. console.log(story.author.name); // prints "Ian Fleming"
  7. });

Checking Whether a Field is Populated

You can call the populated() function to check whether a field is populated. If populated() returns a truthy value, you can assume the field is populated.

  1. story.populated('author'); // truthy
  2. story.depopulate('author'); // Make `author` not populated anymore
  3. story.populated('author'); // undefined

A common reason for checking whether a path is populated is getting the author id. However, for your convenience, Mongoose adds a _id getter to ObjectId instances so you can use story.author._id regardless of whether author is populated.

  1. story.populated('author'); // truthy
  2. story.author._id; // ObjectId
  3. story.depopulate('author'); // Make `author` not populated anymore
  4. story.populated('author'); // undefined
  5. story.author instanceof ObjectId; // true
  6. story.author._id; // ObjectId, because Mongoose adds a special getter

What If There’s No Foreign Document?

Mongoose populate doesn’t behave like conventional SQL joins. When there’s no document, story.author will be null. This is analogous to a left join in SQL.

  1. await Person.deleteMany({ name: 'Ian Fleming' });
  2. const story = await Story.findOne({ title: 'Casino Royale' }).populate('author');
  3. story.author; // `null`

If you have an array of authors in your storySchema, populate() will give you an empty array instead.

  1. const storySchema = Schema({
  2. authors: [{ type: Schema.Types.ObjectId, ref: 'Person' }],
  3. title: String
  4. });
  5. // Later
  6. const story = await Story.findOne({ title: 'Casino Royale' }).populate('authors');
  7. story.authors; // `[]`

Field Selection

What if we only want a few specific fields returned for the populated documents? This can be accomplished by passing the usual field name syntax as the second argument to the populate method:

  1. Story.
  2. findOne({ title: /casino royale/i }).
  3. populate('author', 'name'). // only return the Persons name
  4. exec(function (err, story) {
  5. if (err) return handleError(err);
  6. console.log('The author is %s', story.author.name);
  7. // prints "The author is Ian Fleming"
  8. console.log('The authors age is %s', story.author.age);
  9. // prints "The authors age is null"
  10. });

Populating Multiple Paths

What if we wanted to populate multiple paths at the same time?

  1. Story.
  2. find(...).
  3. populate('fans').
  4. populate('author').
  5. exec();

If you call populate() multiple times with the same path, only the last one will take effect.

  1. // The 2nd `populate()` call below overwrites the first because they
  2. // both populate 'fans'.
  3. Story.
  4. find().
  5. populate({ path: 'fans', select: 'name' }).
  6. populate({ path: 'fans', select: 'email' });
  7. // The above is equivalent to:
  8. Story.find().populate({ path: 'fans', select: 'email' });

Query conditions and other options

What if we wanted to populate our fans array based on their age and select just their names?

  1. Story.
  2. find().
  3. populate({
  4. path: 'fans',
  5. match: { age: { $gte: 21 } },
  6. // Explicitly exclude `_id`, see http://bit.ly/2aEfTdB
  7. select: 'name -_id'
  8. }).
  9. exec();

The match option doesn’t filter out Story documents. If there are no documents that satisfy match, you’ll get a Story document with an empty fans array.

For example, suppose you populate() a story’s author and the author doesn’t satisfy match. Then the story’s author will be null.

  1. const story = await Story.
  2. findOne({ title: 'Casino Royale' }).
  3. populate({ path: 'author', name: { $ne: 'Ian Fleming' } }).
  4. exec();
  5. story.author; // `null`

In general, there is no way to make populate() filter stories based on properties of the story’s author. For example, the below query won’t return any results, even though author is populated.

  1. const story = await Story.
  2. findOne({ 'author.name': 'Ian Fleming' }).
  3. populate('author').
  4. exec();
  5. story; // null

If you want to filter stories by their author’s name, you should use denormalization.

limit vs. perDocumentLimit

Populate does support a limit option, however, it currently does not limit on a per-document basis for backwards compatibility. For example, suppose you have 2 stories:

  1. Story.create([
  2. { title: 'Casino Royale', fans: [1, 2, 3, 4, 5, 6, 7, 8] },
  3. { title: 'Live and Let Die', fans: [9, 10] }
  4. ]);

If you were to populate() using the limit option, you would find that the 2nd story has 0 fans:

  1. const stories = Story.find().populate({
  2. path: 'fans',
  3. options: { limit: 2 }
  4. });
  5. stories[0].name; // 'Casino Royale'
  6. stories[0].fans.length; // 2
  7. // 2nd story has 0 fans!
  8. stories[1].name; // 'Live and Let Die'
  9. stories[1].fans.length; // 0

That’s because, in order to avoid executing a separate query for each document, Mongoose instead queries for fans using numDocuments * limit as the limit. If you need the correct limit, you should use the perDocumentLimit option (new in Mongoose 5.9.0). Just keep in mind that populate() will execute a separate query for each story, which may cause populate() to be slower.

  1. const stories = await Story.find().populate({
  2. path: 'fans',
  3. // Special option that tells Mongoose to execute a separate query
  4. // for each `story` to make sure we get 2 fans for each story.
  5. perDocumentLimit: 2
  6. });
  7. stories[0].name; // 'Casino Royale'
  8. stories[0].fans.length; // 2
  9. stories[1].name; // 'Live and Let Die'
  10. stories[1].fans.length; // 2

Refs to children

We may find however, if we use the author object, we are unable to get a list of the stories. This is because no story objects were ever ‘pushed’ onto author.stories.

There are two perspectives here. First, you may want the author know which stories are theirs. Usually, your schema should resolve one-to-many relationships by having a parent pointer in the ‘many’ side. But, if you have a good reason to want an array of child pointers, you can push() documents onto the array as shown below.

  1. author.stories.push(story1);
  2. author.save(callback);

This allows us to perform a find and populate combo:

  1. Person.
  2. findOne({ name: 'Ian Fleming' }).
  3. populate('stories'). // only works if we pushed refs to children
  4. exec(function (err, person) {
  5. if (err) return handleError(err);
  6. console.log(person);
  7. });

It is debatable that we really want two sets of pointers as they may get out of sync. Instead we could skip populating and directly find() the stories we are interested in.

  1. Story.
  2. find({ author: author._id }).
  3. exec(function (err, stories) {
  4. if (err) return handleError(err);
  5. console.log('The stories are an array: ', stories);
  6. });

The documents returned from query population become fully functional, removeable, saveable documents unless the lean option is specified. Do not confuse them with sub docs. Take caution when calling its remove method because you’ll be removing it from the database, not just the array.

Populating an existing document

If you have an existing mongoose document and want to populate some of its paths, you can use the Document#populate() method. Just make sure you call Document#execPopulate() to execute the populate().

  1. const person = await Person.findOne({ name: 'Ian Fleming' });
  2. person.populated('stories'); // null
  3. // Call the `populate()` method on a document to populate a path.
  4. // Need to call `execPopulate()` to actually execute the `populate()`.
  5. await person.populate('stories').execPopulate();
  6. person.populated('stories'); // Array of ObjectIds
  7. person.stories[0].name; // 'Casino Royale'

The Document#populate() method supports chaining, so you can chain multiple populate() calls together.

  1. await person.populate('stories').populate('fans').execPopulate();
  2. person.populated('fans'); // Array of ObjectIds

Populating multiple existing documents

If we have one or many mongoose documents or even plain objects (like mapReduce output), we may populate them using the Model.populate() method. This is what Document#populate() and Query#populate() use to populate documents.

Populating across multiple levels

Say you have a user schema which keeps track of the user’s friends.

  1. const userSchema = new Schema({
  2. name: String,
  3. friends: [{ type: ObjectId, ref: 'User' }]
  4. });

Populate lets you get a list of a user’s friends, but what if you also wanted a user’s friends of friends? Specify the populate option to tell mongoose to populate the friends array of all the user’s friends:

  1. User.
  2. findOne({ name: 'Val' }).
  3. populate({
  4. path: 'friends',
  5. // Get friends of friends - populate the 'friends' array for every friend
  6. populate: { path: 'friends' }
  7. });

Cross Database Populate

Let’s say you have a schema representing events, and a schema representing conversations. Each event has a corresponding conversation thread.

  1. const db1 = mongoose.createConnection('mongodb://localhost:27000/db1');
  2. const db2 = mongoose.createConnection('mongodb://localhost:27001/db2');
  3. const conversationSchema = new Schema({ numMessages: Number });
  4. const Conversation = db2.model('Conversation', conversationSchema);
  5. const eventSchema = new Schema({
  6. name: String,
  7. conversation: {
  8. type: ObjectId,
  9. ref: Conversation // `ref` is a **Model class**, not a string
  10. }
  11. });
  12. const Event = db1.model('Event', eventSchema);

In the above example, events and conversations are stored in separate MongoDB databases. String ref will not work in this situation, because Mongoose assumes a string ref refers to a model name on the same connection. In the above example, the conversation model is registered on db2, not db1.

  1. // Works
  2. const events = await Event.
  3. find().
  4. populate('conversation');

This is known as a “cross-database populate,” because it enables you to populate across MongoDB databases and even across MongoDB instances.

If you don’t have access to the model instance when defining your eventSchema, you can also pass the model instance as an option to populate().

  1. const events = await Event.
  2. find().
  3. // The `model` option specifies the model to use for populating.
  4. populate({ path: 'conversation', model: Conversation });

Dynamic References via `refPath`

Mongoose can also populate from multiple collections based on the value of a property in the document. Let’s say you’re building a schema for storing comments. A user may comment on either a blog post or a product.

  1. const commentSchema = new Schema({
  2. body: { type: String, required: true },
  3. on: {
  4. type: Schema.Types.ObjectId,
  5. required: true,
  6. // Instead of a hardcoded model name in `ref`, `refPath` means Mongoose
  7. // will look at the `onModel` property to find the right model.
  8. refPath: 'onModel'
  9. },
  10. onModel: {
  11. type: String,
  12. required: true,
  13. enum: ['BlogPost', 'Product']
  14. }
  15. });
  16. const Product = mongoose.model('Product', new Schema({ name: String }));
  17. const BlogPost = mongoose.model('BlogPost', new Schema({ title: String }));
  18. const Comment = mongoose.model('Comment', commentSchema);

The refPath option is a more sophisticated alternative to ref. If ref is just a string, Mongoose will always query the same model to find the populated subdocs. With refPath, you can configure what model Mongoose uses for each document.

  1. const book = await Product.create({ name: 'The Count of Monte Cristo' });
  2. const post = await BlogPost.create({ title: 'Top 10 French Novels' });
  3. const commentOnBook = await Comment.create({
  4. body: 'Great read',
  5. on: book._id,
  6. onModel: 'Product'
  7. });
  8. const commentOnPost = await Comment.create({
  9. body: 'Very informative',
  10. on: post._id,
  11. onModel: 'BlogPost'
  12. });
  13. // The below `populate()` works even though one comment references the
  14. // 'Product' collection and the other references the 'BlogPost' collection.
  15. const comments = await Comment.find().populate('on').sort({ body: 1 });
  16. comments[0].on.name; // "The Count of Monte Cristo"
  17. comments[1].on.title; // "Top 10 French Novels"

An alternative approach is to define separate blogPost and product properties on commentSchema, and then populate() on both properties.

  1. const commentSchema = new Schema({
  2. body: { type: String, required: true },
  3. product: {
  4. type: Schema.Types.ObjectId,
  5. required: true,
  6. ref: 'Product'
  7. },
  8. blogPost: {
  9. type: Schema.Types.ObjectId,
  10. required: true,
  11. ref: 'BlogPost'
  12. }
  13. });
  14. // ...
  15. // The below `populate()` is equivalent to the `refPath` approach, you
  16. // just need to make sure you `populate()` both `product` and `blogPost`.
  17. const comments = await Comment.find().
  18. populate('product').
  19. populate('blogPost').
  20. sort({ body: 1 });
  21. comments[0].product.name; // "The Count of Monte Cristo"
  22. comments[1].blogPost.title; // "Top 10 French Novels"

Defining separate blogPost and product properties works for this simple example. But, if you decide to allow users to also comment on articles or other comments, you’ll need to add more properties to your schema. You’ll also need an extra populate() call for every property, unless you use mongoose-autopopulate. Using refPath means you only need 2 schema paths and one populate() call regardless of how many models your commentSchema can point to.

Populate Virtuals

So far you’ve only populated based on the _id field. However, that’s sometimes not the right choice. In particular, arrays that grow without bound are a MongoDB anti-pattern. Using mongoose virtuals, you can define more sophisticated relationships between documents.

  1. const PersonSchema = new Schema({
  2. name: String,
  3. band: String
  4. });
  5. const BandSchema = new Schema({
  6. name: String
  7. });
  8. BandSchema.virtual('members', {
  9. ref: 'Person', // The model to use
  10. localField: 'name', // Find people where `localField`
  11. foreignField: 'band', // is equal to `foreignField`
  12. // If `justOne` is true, 'members' will be a single doc as opposed to
  13. // an array. `justOne` is false by default.
  14. justOne: false,
  15. options: { sort: { name: -1 }, limit: 5 } // Query options, see http://bit.ly/mongoose-query-options
  16. });
  17. const Person = mongoose.model('Person', PersonSchema);
  18. const Band = mongoose.model('Band', BandSchema);
  19. /**
  20. * Suppose you have 2 bands: "Guns N' Roses" and "Motley Crue"
  21. * And 4 people: "Axl Rose" and "Slash" with "Guns N' Roses", and
  22. * "Vince Neil" and "Nikki Sixx" with "Motley Crue"
  23. */
  24. Band.find({}).populate('members').exec(function(error, bands) {
  25. /* `bands.members` is now an array of instances of `Person` */
  26. });

You can also use the populate match option to add an additional filter to the populate() query. This is useful if you need to split up populate() data:

  1. const PersonSchema = new Schema({
  2. name: String,
  3. band: String,
  4. isActive: Boolean
  5. });
  6. const BandSchema = new Schema({
  7. name: String
  8. });
  9. BandSchema.virtual('activeMembers', {
  10. ref: 'Person',
  11. localField: 'name',
  12. foreignField: 'band',
  13. justOne: false,
  14. match: { isActive: true }
  15. });
  16. BandSchema.virtual('formerMembers', {
  17. ref: 'Person',
  18. localField: 'name',
  19. foreignField: 'band',
  20. justOne: false,
  21. match: { isActive: false }
  22. });

Keep in mind that virtuals are not included in toJSON() and toObject() output by default. If you want populate virtuals to show up when using functions that rely on JSON.stringify(), like Express’ res.json() function, set the virtuals: true option on your schema’s toJSON options.

  1. const BandSchema = new Schema({ name: String }, {
  2. toJSON: { virtuals: true }, // So `res.json()` and other `JSON.stringify()` functions include virtuals
  3. toObject: { virtuals: true } // So `toObject()` output includes virtuals
  4. });

If you’re using populate projections, make sure foreignField is included in the projection.

  1. Band.
  2. find({}).
  3. populate({ path: 'members', select: 'name' }).
  4. exec(function(error, bands) {
  5. // Won't work, foreign field `band` is not selected in the projection
  6. });
  7. Band.
  8. find({}).
  9. populate({ path: 'members', select: 'name band' }).
  10. exec(function(error, bands) {
  11. // Works, foreign field `band` is selected
  12. });

Populate Virtuals: The Count Option

Populate virtuals also support counting the number of documents with matching foreignField as opposed to the documents themselves. Set the count option on your virtual:

  1. const PersonSchema = new Schema({
  2. name: String,
  3. band: String
  4. });
  5. const BandSchema = new Schema({
  6. name: String
  7. });
  8. BandSchema.virtual('numMembers', {
  9. ref: 'Person', // The model to use
  10. localField: 'name', // Find people where `localField`
  11. foreignField: 'band', // is equal to `foreignField`
  12. count: true // And only get the number of docs
  13. });
  14. // Later
  15. const doc = await Band.findOne({ name: 'Motley Crue' }).
  16. populate('numMembers');
  17. doc.numMembers; // 2

Populating Maps

Maps are a type that represents an object with arbitrary string keys. For example, in the below schema, members is a map from strings to ObjectIds.

  1. const BandSchema = new Schema({
  2. name: String,
  3. members: {
  4. type: Map,
  5. of: {
  6. type: 'ObjectId',
  7. ref: 'Person'
  8. }
  9. }
  10. });
  11. const Band = mongoose.model('Band', bandSchema);

This map has a ref, which means you can use populate() to populate all the ObjectIds in the map. Suppose you have the below band document:

  1. const person1 = new Person({ name: 'Vince Neil' });
  2. const person2 = new Person({ name: 'Mick Mars' });
  3. const band = new Band({
  4. name: 'Motley Crue',
  5. members: {
  6. 'singer': person1._id,
  7. 'guitarist': person2._id
  8. }
  9. });

You can populate() every element in the map by populating the special path members.$*. $* is a special syntax that tells Mongoose to look at every key in the map.

  1. const band = await Band.findOne({ name: 'Motley Crue' }).populate('members.$*');
  2. band.members.get('singer'); // { _id: ..., name: 'Vince Neil' }

You can also populate paths in maps of subdocuments using $*. For example, suppose you have the below librarySchema:

  1. const librarySchema = new Schema({
  2. name: String,
  3. books: {
  4. type: Map,
  5. of: new Schema({
  6. title: String,
  7. author: {
  8. type: 'ObjectId',
  9. ref: 'Person'
  10. }
  11. })
  12. }
  13. });
  14. const Library = mongoose.model('Library, librarySchema');

You can populate() every book’s author by populating books.$*.author:

  1. const libraries = await Library.find().populate('books.$*.author');

Populate in Middleware

You can populate in either pre or post hooks. If you want to always populate a certain field, check out the mongoose-autopopulate plugin.

  1. // Always attach `populate()` to `find()` calls
  2. MySchema.pre('find', function() {
  3. this.populate('user');
  4. });
  1. // Always `populate()` after `find()` calls. Useful if you want to selectively populate
  2. // based on the docs found.
  3. MySchema.post('find', async function(docs) {
  4. for (let doc of docs) {
  5. if (doc.isPublic) {
  6. await doc.populate('user').execPopulate();
  7. }
  8. }
  9. });
  1. // `populate()` after saving. Useful for sending populated data back to the client in an
  2. // update API endpoint
  3. MySchema.post('save', function(doc, next) {
  4. doc.populate('user').execPopulate().then(function() {
  5. next();
  6. });
  7. });

Next Up

Now that we’ve covered populate(), let’s take a look at discriminators.