【问题标题】:How to relate user collection with other data collection in mongoose?如何将用户收集与猫鼬中的其他数据收集联系起来?
【发布时间】:2021-03-24 03:54:09
【问题描述】:

我正在使用 node.js 并使用 mongoose 数据库制作一个待办事项列表网站。在这个数据库中,我有两个两个集合,

  1. 具有如下架构的用户集合: const userSchema = new mongoose.Schema({ 电子邮件:字符串, 密码:字符串, googleId:字符串 });
  2. 列表集合,用于存储具有如下架构的列表项: 常量 itemsSchema = { 名称:字符串 } 如何确保如果用户已登录,那么它在 todolist 中输入的项目只属于他/她?

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    在 user.js 中添加:

    module.exports = mongoose.model('User', userSchema)
    
    const mongoose = require('mongoose')
    
    const itemsSchema = { 
      name: String,
      user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-25
      • 2021-11-06
      • 2015-03-18
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-26
      • 1970-01-01
      相关资源
      最近更新 更多