DEV Community

loading...

#12 Relational Data

rittwickbhabak profile image Rittwick Bhabak ・1 min read

Suppose, Author(name, age, books) and Book(title, pages) are two relations.
In the following way we construct it in MongoDB

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const BookSchema = new Schema({
    title: String,
    pages: Number 
});

const AuthorSchema = new Schema({
    name: String,
    age: Number, 
    books: [BookSchema] 
})

const Author = mongoose.model('author', AuthorSchema);

module.exports = Author;
Enter fullscreen mode Exit fullscreen mode

books: [BookSchema] tells us that books property is an array where the array elements will be of the form BookSchema

Discussion (0)

pic
Editor guide