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

375
Views
mongoose population (refs) is not working

I have created two models where I am trying to ref one model to another but it's not working and I don't have any idea about it I used it exactly the same way as in the mongoose example but it is not helping, the only difference between my working and their example is I have two different files for both models and I am exporting them to use in another file

The example I took reference from Mongoose

First Model

const userModel = require('./userModel')
const {Schema} = mongoose

const tweet = new Schema({
    date: {
        type: Date,
        required: true,
    },
    msg:{
        type: String,
        maxlength:140,
        required:true
    },
    tweetid:{
        type: mongoose.Types.ObjectId
    }
})

const tweetSchema = new Schema({
    user: {
        type: Schema.Types.ObjectId,
        ref: 'userModel'
    },
    tweets: {
        type: [{tweet}],
        default: []
    }
})

const tweetModel = mongoose.model('tweet',tweetSchema)

module.exports = tweetModel

Second Model

const {Schema} = mongoose

const userSchema = new Schema({
    email: {
        type:String,
        required: true,
        unique: true,
        trim:true
    },
    password: {
        type:String,
        required: true,
        trim:true
    },
    follows: {
        type: [String],
        default: []
    }
})

const userModel = mongoose.model("User",userSchema) 

module.exports = userModel
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Your reference string should be equal to the model name that you have passed to mongoose.model:

const tweetSchema = new Schema({
    user: {
        type: Schema.Types.ObjectId,
        ref: 'User'
    },
    tweets: {
        type: [{tweet}],
        default: []
    }
})
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!