'Populate Failed immediately a ref was indexed in Mongodb via Mongoose Schema

I had a Post Model with a ref as below

const mongoose = require("mongoose");

const PostSchema = new mongoose.Schema(
    {
        _id: mongoose.Schema.Types.ObjectId,
        identifier: String,
        workSpace: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Workspace'
        }
    }
);

My Post queries were so slow so I attempted to index the workSpace property as below:

  const PostSchema = new mongoose.Schema(
            {
                _id: mongoose.Schema.Types.ObjectId,
                identifier: String,
                workSpace: {
                    type: mongoose.Schema.Types.ObjectId,
                    ref: 'Workspace',
                    index:1 //I added this line to the Schema and everything went in disarray in production
                }
            }
        );

The moment I added that new index field into the ref the following calls started failing

let posts = await Post.find({published:false}).populate('workSpace');

workSpace was now coming back as null in all the results returned from `posts

Which got me thinking, if a ref is indexed as above, will populate fail to work? Thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source