Navneet Tagore
Navneet Tagore

Reputation: 1

How to relate user collection with other data collection in mongoose?

I am making a Todo List website with node.js and using mongoose database. In this database I have two two collecions,

  1. user colllection having schema like this : const userSchema = new mongoose.Schema({ email: String, password: String, googleId: String });
  2. list collection to store list items having schema like : const itemsSchema = { name: String } How to ensure that if a user is logged in then the item it enters in todolist belogs to him/her only ?

Upvotes: 0

Views: 429

Answers (1)

kamil-now
kamil-now

Reputation: 79

In user.js add:

module.exports = mongoose.model('User', userSchema)
const mongoose = require('mongoose')

const itemsSchema = { 
  name: String,
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User'
  }
}

Upvotes: 1

Related Questions