28

Is it possible to populate a mongoose model with a field of a reference model that isn't the _id ... e.g. a username.

so something like

var personSchema = Schema({
  _id     : Number,
  name    : String,
  age     : Number,
  stories : { type: String, field: "username", ref: 'Story' }
});
1
  • 2
    Fast forward to 2017, and Frosty's answer should be the accepted answer. Jan 31, 2017 at 1:34

5 Answers 5

101

This is supported since Mongoose 4.5, and is called virtuals population.

You have to define your foreign keys relationships after your schemas definitions and before creating models, like this:

// Schema definitions

BookSchema = new mongoose.Schema({
        ...,
        title: String,
        authorId: Number,
        ...
    },
    // schema options: Don't forget this option
    // if you declare foreign keys for this schema afterwards.
    {
        toObject: {virtuals:true},
        // use if your results might be retrieved as JSON
        // see http://stackoverflow.com/q/13133911/488666
        //toJSON: {virtuals:true} 
    });

PersonSchema = new mongoose.Schema({id: Number, ...});


// Foreign keys definitions

BookSchema.virtual('author', {
  ref: 'Person',
  localField: 'authorId',
  foreignField: 'id',
  justOne: true // for many-to-1 relationships
});


// Models creation

var Book = mongoose.model('Book', BookSchema);
var Person = mongoose.model('Person', PersonSchema);


// Querying

Book.find({...})
    // if you use select() be sure to include the foreign key field !
    .select({.... authorId ....}) 
    // use the 'virtual population' name
    .populate('author')
    .exec(function(err, books) {...})
5
3

It seems they enforce to use _id, and maybe we can customize it in the future.

Here is the issue on Github https://github.com/LearnBoost/mongoose/issues/2562

1
3

This is an example of using the $lookup aggregate to populate a model called Invite with the respective User based on the corresponding email field:

  Invite.aggregate(
      { $match: {interview: req.params.interview}},
      { $lookup: {from: 'users', localField: 'email', foreignField: 'email', as: 'user'} }
    ).exec( function (err, invites) {
      if (err) {
        next(err);
      }

      res.json(invites);
    }
  );

It's probably quite similar to what you're trying to do.

0

To add to Frosty's answer, if you're looking to refer to an array of documents of another collection you would make changes like so.

BookSchema = new mongoose.Schema(
{
    title: String,
    authorId: [Number],
},
// schema options: Don't forget this option
// if you declare foreign keys for this schema afterwards.
{
    toObject: { virtuals: true },
    // use if your results might be retrieved as JSON
    // see http://stackoverflow.com/q/13133911/488666
    toJSON: {virtuals:true}
});

PersonSchema = new mongoose.Schema({ id: Number, name: String });

BookSchema.virtual("author", {
ref: "Person",
localField: ["authorId"],
foreignField: ["id"],
// justOne: true, // Needs to be commented out in this scenario, 
});
-6

You may use the populate() API. The API is more flexible, you don't have to specify ref and field in the Schema.

http://mongoosejs.com/docs/api.html#document_Document-populate http://mongoosejs.com/docs/api.html#model_Model.populate

You can mix and match with find().

3
  • 4
    I can't understand from the docs. Could you give an example query? May 14, 2014 at 13:01
  • 8
    Coming to SO only after reading the documentation, if I understood it I wouldn't search for answers here.
    – Azder
    Jul 24, 2014 at 5:58
  • You still need to configure your Schema for populate to be use, by adding reference.
    – lukaserat
    Sep 9, 2016 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.