'how to filter a value between two columns with index

I currently have a schema where I have two columns of zipCode, the initial and the final as follows:

const MongoSchema = new mongoose.Schema({
  data: [
   id: Number,
   weight: Number,
   lenght: Number,
   zipcodeStart: Number,
   zipcodeEnd: Number,
  ]
});

I created the index as follows:

MongoSchema.index({ zipcodeStart: 1, zipcodeEnd: 1 });

my problem is I need to pass a value that is between zipCodeStart and zipCodeEnd and filter all elements that have a zipCode between the start and the end using index

the query I tried was the following:

return this.connection
      .find({ zipcodeStart_1_zipcodeEnd_1: { $all: [zipCode] } })
      .skip(page)
      .limit(pagination.perPage * 1);

but I always get an empty array.

[]

can you help me?



Sources

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

Source: Stack Overflow

Solution Source