如何在两个集合之间创建关系



我有两个猫鼬模型: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,
},
});

您可以使用猫鼬种群

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

最新更新