Friday, 26 May 2017

Adding realationship with 2 schemas with mongoose and mongodb

I need to have some relationships between two schema. like a relationship between User and comments. This is the code i have tried.

const user = new Schema({
    uname:String,
    email:String,
    password:String,
    created:{ type: Date, default: Date.now }
});

const comment = new Schema({
    user:{
        type : Schema.Types.ObjectId,
        ref : 'user'
    },
    comment:String,
    created:{ type: Date, default: Date.now }
});

How should i save data to comment? Do i have to pass the ID of the users whose commenting? or can i pass any unique value (email or username).How to pass data and retrieve comment data?. Im quite new to NoSQL databases.



via Madushika Perera

No comments:

Post a Comment