Schema which has reference to alternate schema?

193 views
Skip to first unread message

Syrkres Vlos

unread,
Mar 23, 2017, 9:18:12 AM3/23/17
to Mongoose Node.JS ODM
Well met,

Just started with nodejs/mongoose and come from relation DB background.
My question is on the example locationSchema below.
I have a PersonData, OrganizationData and LocationData. 

A LocationData can be controlled by a PersonData or an Organizationdata? How can I do this?


var personSchema = new Schema({
_id: {type: String, required: true},
name: {type: String, required: true},
race: String,
class: String
});
var PersonData = mongoose.model('PersonData', personSchema );

var organizationSchema = new Schema({
_id: {type: String, required: true},
name: {type: String, required: true},
category: String,
structure: String,
members: [{ type: String, ref: 'PersonData'}]
});
var OrganizationData = mongoose.model('OrganizationData', organizationSchema ); 

var locationSchema = new Schema({
    _id: {type: String, required: true},
name: {type: String, required: true},
controlledBy:{
            type: String, enum: [ 'Organization', 'Individual'],
            controller: { type: String, ref: 'PersonData' or 'OrganizationData'}
    }
});


Thanks for assistance.

Rob Colbert

unread,
Mar 23, 2017, 8:37:35 PM3/23/17
to Mongoose Node.JS ODM
Documentation link. Mongoose has the logic you want. Instead of using ref, use refPath and specify the path of the field that holds the name of the Model to ref.

In your example, if you were to change your controlledBy.type enum to match the names required ['PersonData', 'OrganizationData'], you can simply set controlledBy.type to the name of the model.

Separately, unless you actually mean to have each document's _id field be a String, it's probably best to not declare it. If you must, Schema.ObjectId is the correct type to use for document._id.

Syrkres Vlos

unread,
Mar 24, 2017, 8:46:17 AM3/24/17
to Mongoose Node.JS ODM
RC, thanks a ton for the information, guess my google foo is not what it used to be.

In looking at the documentation link you posted, I believe what  I would need in my model is as follows:

var locationSchema = new Schema({
    _id: {type: String, required: true},
name: {type: String, required: true},
controlledBy:{
            kind: {type: String, enum: [ 'OrganizationData', 'IndividualData']},  
            controller: {type: ObjectId, refPath: 'controlledBy.kind'},
    }
});


Rob Colbert

unread,
Mar 24, 2017, 8:49:17 AM3/24/17
to mongoo...@googlegroups.com
Yes. That looks correct. You set refPath to the dotted notation of the field in the document that names the collection to reference by value (string). After, when populating, Mongoose considers the value of the field as the name of the model from which to populate (by _id).

--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to a topic in the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongoose-orm/km3rBB9UidQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongoose-orm+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Syrkres Vlos

unread,
Mar 24, 2017, 9:30:50 AM3/24/17
to Mongoose Node.JS ODM
Sorry for another ask, but maybe it's straight forward as ref save/update?

assuming on my save method I have the following:?

var person = new PersonData{
name: req.body.personName,
_id: ObjectId(1)
}
var location = {
_id:
ObjectId(123),
name: req.body.name,
controlledBy: {
kind: 'IndividualData',
controller: person._id
}
}

var data = new LocationData(location);
data.save(function (err, location) {
if (err) return console.error(err);


person.save(function (err) {
if (err) return console.error(err);
});
res.redirect('/get-dataLocation');
});

Or do I need to do something specific for the refPath?

Rob Colbert

unread,
Mar 24, 2017, 2:13:02 PM3/24/17
to mongoo...@googlegroups.com
No problem, sorry for for lag :) You are correct. All you need is to set the field pointed to by refPath to the name of the model to be used when referencing. Internally, Mongoose is basically using the string in that field as a parameter to a populate call on your behalf. Nothing special/further is needed :)

--
Reply all
Reply to author
Forward
0 new messages