In this example, they create personSchema
using ObjectId
to reference the Story
and this I understand. But then in storySchema
why don't they do the same to reference the person?
Or the inverse: why using ObjectId instead of Number in Person?
var mongoose = require('mongoose')
, Schema = mongoose.Schema
var personSchema = Schema({
_id : Number,
name : String,
age : Number,
stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});
var storySchema = Schema({
_creator : { type: Number, ref: 'Person' },
title : String,
fans : [{ type: Number, ref: 'Person' }]
});
var Story = mongoose.model('Story', storySchema);
var Person = mongoose.model('Person', personSchema);
Type of reference has to be the same as the referenced schema's
_id
property.In case of
personSchema
it's aNumber
.storySchema
on the other hand, has the_id
field assigned automatically by mongoose - it's not specified in parameters for the schema constructor.