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;
star

Fri May 21 2021 16:36:01 GMT+0000 (Coordinated Universal Time)

#mongoose #schema #indexing #checkifworks
star

Fri May 21 2021 16:33:23 GMT+0000 (Coordinated Universal Time)

#mongoose #schema #indexing

Save snippets that work with our extensions

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