Emad Baqeri
Emad Baqeri

Reputation: 2692

using $gte and $lt for picking different rages of a specific field value

I'm trying to pick documents with cost fields between 0 and 5 then trying to pick documents with cost ranges between 10 and 15. For this, I'm using MongoDB aggregation

    const something = await dummyModel.aggregate([
      {
        $match: {
          fieldA: {
            $in: arryOfIds,
          },
          cost: {
            $or: [
              { $gte: 0, $lt: 5 },
              { $gte: 10, $lt: 15 },
            ],
          },
        },
      },
    ]);

With the above approach, I'm getting this error from mongoose.

"message": "unknown operator: $or",

Any Idea on this to pick ranges in MongoDB using mongoose

Upvotes: 1

Views: 880

Answers (1)

NeNaD
NeNaD

Reputation: 20334

You should do it with find query.

db.collection.find({
  "fieldA": {
    "$in": arryOfIds,
  },
  "$or": [
    {
      "cost": {
        "$gte": 0,
        "$lt": 5
      }
    },
    {
      "cost": {
        "$gte": 10,
        "$lt": 15
      }
    }
  ]
})

Working example

Upvotes: 1

Related Questions