DEV Community

Rittwick Bhabak
Rittwick Bhabak

Posted on

#12 Relational Data

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

Top comments (0)