Snippets Collections
// To check which fields are indexed
Product.collection
  .getIndexes({ full: true })
  .then((indexes) => {
    console.log('indexes:', indexes);
  })
  .catch(console.error);

// To check if indexing works
(async () => {
  const result = await Product.find(
    { friendlyName: 'Nice other Name' },
    '_id'
  ).explain('executionStats');
  console.log(result);
})();
const mongoose = require('mongoose');

const ProductSchema = new mongoose.Schema(
  {
    _id: mongoose.Schema.Types.ObjectId,
    isComplete: { type: Boolean },
    isActive: { type: Boolean },
    friendlyName: { type: String, index: true },//This is the important bit +
    subscription: {
      _id: false,
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Subscription',
    },

    productTypeId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'ProductType',
      required: true,
    },

    columns: { type: Array, columnName: String, value: String },
  },
  { timestamps: true }
);

ProductSchema.index({ friendlyName: 1 });//This is also important bit

const Product = mongoose.model('Product', ProductSchema);

module.exports = Product;

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension