Home > Mobile >  Second Match is working not the 1st one MongoDB Aggregation
Second Match is working not the 1st one MongoDB Aggregation

Time:08-22

     let id = new moongoes.Types.ObjectID("some_id_here")

      aggregation = PropertyDetails.aggregate([
            {
            $match: {
              team: id,
              isDeleted: { $in:[ null, "", false] }
            },
              $match: {
              units: {
                $gte: 20,
                $lte: 25
              }
          }
          
        }]) 

Using the first match works fine getting the result against ID but when trying to get between Units using the second match it's returning all the units between 20 and 25 regardless of the ID I selected in First Match

CodePudding user response:

It should work like you did that. I made a test

[
  {
    '$match': {
      'team': 12345, 
      'isDeleted': {
        '$in': [
          null, '', false
        ]
      }
    }
  }, {
    '$match': {
      'units': {
        '$gte': 20, 
        '$lte': 25
      }
    }
  }
]

You can see - team 12346 is not included in the results.

enter image description here

But why arent you using a single stage for this?

[
  {
    '$match': {
      'team': 12345, 
      'isDeleted': {
        '$in': [
          null, '', false
        ]
      }, 
      'units': {
        '$gte': 20, 
        '$lte': 25
      }
    }
  }
]

This would lead to the same results.

  • Related