Given this Category Schema
{
title: String,
parent: {
type: Schema.Types.ObjectId,
ref: 'Category',
required: true
}
}
Now what if I have an instance of Category, but I don't know if parent has been populated or not and I need the _id (actually just the hex representation) of the parent.
Is there a nicer way of doing this: ?
var parentID = (category.parent._id || category.parent).toString();
I don't think there's a nicer way. I did put this in a virtual though.
schema.virtual('parentID').get(function() {
return this.parent._id || this.parent;
})
This could easily be made a plugin if it's needed for multiple schemes.