'Mongo group with condition not working correctly with full text search

I have another issue related to this one

This query

aggregate( [
  { 
      $match: {
          "user_id": 1, 
          "shop_id": 1, 
          "$text": {"$search" : "API"}
      }
  },
  { 
      $group: { 
          _id: null, 
          count: { 
              $sum: { 
                  $cond: [ { $eq: [ "$deleted_at", null ] }, 1, 0 ]  
              } 
          } 
      } 
  }
])

is not working as expected, it is returning 0 as a count value, but should be almost 6k.

But this query (without search)

aggregate( [
  { 
      $match: {
          "user_id": 1, 
          "shop_id": 1
      }
  },
  { 
      $group: { 
          _id: null, 
          count: { 
              $sum: { 
                  $cond: [ { $eq: [ "$deleted_at", null ] }, 1, 0 ]  
              } 
          } 
      } 
  }
])

is working as expected, and also query with search but without $cond

aggregate( [
  { 
      $match: {
          "user_id": 1, 
          "shop_id": 1, 
          "$text": {"$search" : "API"},
          "deleted_at" : {"$eq" : null}
      }
  },
  { $group: { _id: null, count: { $sum: 1 } } }
])

is working as expected, but it's 10 times slower...

Why is the query with search and cond not working?

Thank you in advance



Solution 1:[1]

okay, I found a solution, it works as expected if I replace $eq into $gt, but I do not understand why... why this query

aggregate( [
  { 
      $match: {
          "user_id": 1, 
          "shop_id": 1
      }
  },
  { 
      $group: { 
          _id: null, 
          count: { 
              $sum: { 
                  $cond: [ { $eq: [ "$deleted_at", null ] }, 1, 0 ]  
              } 
          } 
      } 
  }
])

with $eq, but after adding "$text": {"$search" : "API"} it's not working with $eq but working with $gt. So the main issue is solved, but now I have more questions than before... :)

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Bogdan Dubyk