【问题标题】:How to create a relation between two collections?如何在两个集合之间建立关系?
【发布时间】:2021-08-27 08:37:09
【问题描述】:

我有两个猫鼬模型:user.jsnote.js,还有两个集合 notesusers。用户将能够保存他们的笔记。我不知道如何在集合之间建立关系。我希望能够识别笔记和创建笔记的用户,我应该怎么做?

const userSchema = new Schema(
  {
    username: {
      type: String,
      required: [true, "Please enter a username"],
      unique: true,
    },
    password: {
      type: String,
      required: [true, "Please enter a password"],
      minLength: [8, "The minimum password length is 8"],
    },
  },
  { timestamps: true }
);

note.js

const noteSchema = new Schema({
  title: {
    type: String,
  },
  text: {
    type: String,
  },
});

【问题讨论】:

标签: javascript database mongodb mongoose mongoose-schema


【解决方案1】:

你可以使用猫鼬种群

https://mongoosejs.com/docs/populate.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 2018-07-13
    • 2012-05-08
    • 2020-01-17
    • 1970-01-01
    相关资源
    最近更新 更多