Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference another schema in my Mongoose schema?

I'm building a Mongoose schema for a dating app.

I want each person document to contain a reference to all the events they've been to, where events is another schema with its own models in the system. How can I describe this in the schema?

var personSchema = mongoose.Schema({     firstname: String,     lastname: String,     email: String,     gender: {type: String, enum: ["Male", "Female"]}     dob: Date,     city: String,     interests: [interestsSchema],     eventsAttended: ??? }); 
like image 530
CodyBugstein Avatar asked Mar 16 '15 14:03

CodyBugstein


People also ask

What does REF mean in Mongoose schema?

The ref option is what tells Mongoose which model to use during population, in our case the Story model. All _id s we store here must be document _id s from the Story model. Note: ObjectId , Number , String , and Buffer are valid for use as refs.

Can you have a schema within a schema?

Schemas can be nested to represent relationships between objects (e.g. foreign key relationships). For example, a Blog may have an author represented by a User object. Use a Nested field to represent the relationship, passing in a nested schema.

What is Mongoose schema ObjectId?

ObjectId . A SchemaType is just a configuration object for Mongoose. An instance of the mongoose. ObjectId SchemaType doesn't actually create MongoDB ObjectIds, it is just a configuration for a path in a schema.


1 Answers

You can describe it by using Population

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, plain object, multiple plain objects, or all objects returned from a query.

Suppose your Event Schema is defined as follows:

var mongoose = require('mongoose')   , Schema = mongoose.Schema  var eventSchema = Schema({     title     : String,     location  : String,     startDate : Date,     endDate   : Date });  var personSchema = Schema({     firstname: String,     lastname: String,     email: String,     gender: {type: String, enum: ["Male", "Female"]}     dob: Date,     city: String,     interests: [interestsSchema],     eventsAttended: [{ type: Schema.Types.ObjectId, ref: 'Event' }] });  var Event  = mongoose.model('Event', eventSchema); var Person = mongoose.model('Person', personSchema); 

To show how populate is used, first create a person object, aaron = new Person({firstname: 'Aaron'}) and an event object, event1 = new Event({title: 'Hackathon', location: 'foo'}):

aaron.eventsAttended.push(event1); aaron.save(callback);  

Then, when you make your query, you can populate references like this:

Person .findOne({ firstname: 'Aaron' }) .populate('eventsAttended') // only works if we pushed refs to person.eventsAttended .exec(function(err, person) {     if (err) return handleError(err);     console.log(person); }); 
like image 69
chridam Avatar answered Sep 28 '22 03:09

chridam