Search code examples
node.jsmongoosemongoose-populate

How to setup Mongoose Schema for population


I have a problem with my mongoose schemas. I was able to populate one document from another, but I am unable to create a similar connection between other document.

I've been looking at this for a long time but I just don't see whats wrong. It seems to be setup correctly, but comments do not populate. I am using mongoose 5.4.5.

blogSchema

const mongoose = require('mongoose')

const blogSchema = mongoose.Schema({
  title: String,
  author: String,
  url: String,
  likes: Number,
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User'
  },
  comments: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Comment'
    }
  ]
})

blogSchema.set('toJSON', {
  transform: (document, returnedObject) => {
    returnedObject.id = returnedObject._id.toString()
    delete returnedObject._id
    delete returnedObject.__v
  }
})

const Blog = mongoose.model('Blog', blogSchema)

module.exports = Blog

commentSchema

const mongoose = require('mongoose')

const commentSchema = mongoose.Schema({
  text: {
    type: String,
    minlength: 3,
    present: true
  },
  blog: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Blog'
    }
})

commentSchema.set('toJSON', {
  transform: (document, returnedObject) => {
    returnedObject.id = returnedObject._id.toString()
    delete returnedObject._id
    delete returnedObject.__v
  }
})

const Comment = mongoose.model('Comment', commentSchema)

module.exports = Comment

userSchema

const mongoose = require('mongoose')
const uniqueValidator = require('mongoose-unique-validator')

const userSchema = mongoose.Schema({
  username: {
    type: String,
    unique: true,
    minlength: 3,
    present: true
  },
  name: String,
  passwordHash: String,
  blogs: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Blog'
    }
  ],
})

userSchema.plugin(uniqueValidator)

userSchema.set('toJSON', {
  transform: (document, returnedObject) => {
    returnedObject.id = returnedObject._id.toString()
    delete returnedObject._id
    delete returnedObject.__v
    delete returnedObject.passwordHash
  }
})

const User = mongoose.model('User', userSchema)

module.exports = User

populate

router.get('/', async (request, response) => {
  const blogs = await Blog.find({})
    .populate('comment', { text: 1 })
    .populate('user', { username: 1, name: 1 })

  response.json(blogs.map(b => b.toJSON()))
})

I am able to populate user correctly to blogSchema, but populating Comment doesnt work. The order of the populate calls do not change the situation and if I call populate only for comment it doesn't work anyway.

I suppose that there is a problem with my schemas, but I just am unable to see it.


Solution

  • Well...In your blog it's called comments but you try to populate comment. I think that's the issue.