0

I have two mongoose models: user.js and note.js, and two collections notes and users. The users will be able to save their notes. I don't know how to create relations between the collections. I want to be able to identify notes and the user that has created them, how should I do it?

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,
  },
});
  • 1
    You are probably looking for `ref` and population, does this help? https://stackoverflow.com/questions/18001478/referencing-another-schema-in-mongoose – savageGoat Jun 10 '21 at 10:38

1 Answers1

0

You can use mongoose population

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

Nikita Mazur
  • 1,602
  • 1
  • 5
  • 14