Mongoose populate not working?

It appears I have also fallen victim to the all might populate issue.

For me I just get an empty array (I do have data in the collection).

// review.js
import mongoose from "mongoose";

const schema = new mongoose.Schema(
  {
    productId: mongoose.Schema.Types.ObjectId,
    email: String,
    name: String,
    review: String,
    rating: Number,
    gravatar: String
  },
  { timestamps: true, strict: true, strictQuery: true }
);

export default mongoose.model("Reviews", schema);
// product.js
const schema = new mongoose.Schema(
  {
    name: String,
    test: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Reviews'  }],
  },
  { timestamps: true, strict: true, strictQuery: true }
);

export default mongoose.model("products", schema);
import { Product } from "../database/models/index"

const product = await Product.findById(productId).populate('test');

Result:

{
  test: [],
  _id: '63d5ca000a0f69dc24ac8f7c',
  name: 'Tydle Tie Down Straps',

Any help would be great.

I figured this out. I didn’t store the ID in the test array that referenced the review.

2 Likes