Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

277
Views
How to add a model inside another model with mongoose and node js

I am working on a project and am trying to add a model inside another model but i can't seem to figure out how to do it. Have any suggestions? `

const userSchema = new mongoose.Schema({
    first_name: String,
    last_name: String,
    email: String,
    password: String,
});

const ItemSchema = new mongoose.Schema({
    date: String,
    loc: String,
    title: String,
   passage: String,
   file: String
});

const User = mongoose.model("User", userSchema);
const Item = mongoose.model("Item", ItemSchema);

const user1 = new User({
    first_name: "John",
    last_name: "arbuckle",
    email: "blablabal@asdf",
    password: "123456789"
});

const item1 = new Item({
    date: "Today",
    loc: "here",
    title: "message",
    passage: "This is an example",
    file: "none"
});

I am wanting to add this model const Item = mongoose.model("Item", ItemSchema); to the User model.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You are looking for population.

In userSchema add items : { type: [Schema.Types.ObjectId], ref: 'Item' }

const userSchema = new mongoose.Schema({
    first_name: String,
    last_name: String,
    email: String,
    password: String,
    items : {
         type: [Schema.Types.ObjectId],
         ref: 'Item'
    }
});

const ItemSchema = new mongoose.Schema({
    date: String,
    loc: String,
    title: String,
   passage: String,
   file: String
});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!