2

    const writingSchema = {
      title: String,
      content: String
    };
    
    const userSchema = new mongoose.Schema ({
      email: String,
      password: String,
      userID: String,
      userName: String,
      yourWriting: new Array(writingSchema)
    });

Is this the correct format to declare an array inside the object in Node JS

2 Answers 2

2

We can declare an array of subdocuments in Mongoose like:

const userSchema = new mongoose.Schema({
  email: String,
  password: String,
  userID: String,
  userName: String,
  yourWriting: [writingSchema]
});
Sign up to request clarification or add additional context in comments.

Comments

1
const writingSchema = {
    title: String,
    content: String
};

const userSchema = new mongoose.Schema({
    email: String,
    password: String,
    userID: String,
    userName: String,
    yourWriting: [writingSchema]
});

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.