1

I'm new to both MongoDB and Mongoose and trying to figure out the best way to handle creating my schema. I am more used to relational databases, but am hoping this will work with Mongo.

I am creating objects that "extend" other objects. For example, I have a person object that I want to use as the starting point for a parent or grandparent object. Both the parent and grandparent may have additional values beyond those that a base person has (I'm just including one example in each)...

const personSchema = new mongoose.Schema({
    name: String,
    birthDate: Date,
    deathDate: Date,
    });

const parentSchema = new mongoose.Schema({
    //all the stuff a person has + below:
    children: [personSchemas?]  //[array of persons, important... parents can also be children, 
multiple parents will share the same child]
    parentingStyle: String, 
})

const grandParentSchema = new mongoose.Schema({
    // stuff that a parent has plus
    grandparentingStyle: String,
})
jpsnow72
  • 965
  • 2
  • 15
  • 45

1 Answers1

1

I think the following post includes some answers that could be helpful to you: Referencing another schema in Mongoose

Other than that, I might also suggest you'd define parents and grandparents as roles perhaps in this way:

var mongoose = require('mongoose');

const PersonSchema = new mongoose.Schema({
    person_id: {
        type: mongoose.Schema.Types.ObjectId
    }, 
    name: String,
    birthDate: Date,
    deathDate: Date,
    
    //the following is if you want later to fetch persons by role
    role: {
    type: string,
    enum: ["parent", "grandParent","child"], 
    }
    });
//Then you could do it this way 
const ParentSchema = new mongoose.Schema({
    //all the stuff a person has + below:
    children: [personSchemas],
    parentingStyle: String
})

//Or this way 
const ParentSchema = new mongoose.Schema({
    //all the stuff a person has + below:
    children: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Person' 
    }
    parentingStyle: String, 
})

const GrandParentSchema = new mongoose.Schema({
    // same as for parents (ether way)
    // stuff that a parent has plus
    grandparentingStyle: String
})

In non-relational databases, it really depends on what you'd like to do with the data later on. The "joints" (in comaprison to relational DB) are much simpler to create, either by referencing an id or fetching a whole array (like you did) or simply by making more elaborate queries later on.

Jihene-ch
  • 26
  • 6
  • Thanks for your answers. In your suggestion, i see how the child data gets passed to the parent in the children array, but how can I keep the person attributes for that specific parent or do I manually define them. Sort of thinking that my parent inherits person – jpsnow72 Feb 21 '21 at 23:19
  • Well I am not sure I get your question here so I would appreciate it if you'd be more precise but yeah it is a kind of inheritance. I think you could even make a collection of specific children from persons for each parent. I suggest you'd read more about MongoDB before doing anything because you still seem to think in a relational DB way. Please take a look at this post https://stackoverflow.com/questions/5373198/mongodb-relationships-embed-or-reference – Jihene-ch Feb 22 '21 at 08:29