Search code examples
node.jsmongodbmongoose-schema

How do I index a field on a mongoose Schema that uses a discriminator?


The error started after I started using the discriminator.

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const Base = require("../config/Base");

const Refill = Base.discriminator(
  "Refill",
  new Schema({
    cylinderSize: { type: Number, required: true },
    cylinderSwap: { type: Boolean, required: true },
    quantity: { type: Number, required: true },
    location: {
      type: { type: String },
      coordinates: [Number]
    }
  })
);

Refill.index({ location: "2dsphere" });


module.exports = mongoose.model("Refill");

This returns the error Refill.index is not a function


Solution

  • I just took out the Refill.index({ location: "2dsphere" }); and the rest of my code is working fine apparently indexing that field wasn't necessary.