Mongodb - data divergence problem in Aggregation

Asked

Viewed 33 times

-1

I’m making a agreggation in collect pedidos where at the end I make a group to count how many pedidos has every status. So far, so good...

Only when I put it on match looking for a status specific it finds more data than when it is not set value of the status in the match;

[
  {
    '$match': {
      '$and': [
        {
          'created_at': {
            '$gte': new
            Date(
            'Wed, 01 May 2019 00:00:00 GMT'
            ),
            '$lte': new
            Date(
            'Sat, 01 Jun 2019 00:00:00 GMT'
            )
          }
        },
        {
          'status': 'ATIVO'
        },
        {
          '$or': [
            {
              'deleted_at': null
            },
            {
              'deleted_at': {
                '$exists': false
              }
            }
          ]
        }
      ]
    }
  },
  {
    '$sort': {
      'created_at': -1
    }
  },
  {
    '$group': {
      '_id': '$msisdn',
      'order': {
        '$last': '$$ROOT'
      }
    }
  },
  {
    '$match': {
      'order.status': 'ATIVO'
    }
  },
  {
    '$group': {
      '_id': '$order.status',
      'count': {
        '$sum': 1
      }
    }
  }
]

//output
_id:"ATIVO"
count: 3097

[
  {
    '$match': {
      '$and': [
        {
          'created_at': {
            '$gte': new
            Date(
            'Wed, 01 May 2019 00:00:00 GMT'
            ),
            '$lte': new
            Date(
            'Sat, 01 Jun 2019 00:00:00 GMT'
            )
          }
        },
        {
          '$or': [
            {
              'deleted_at': null
            },
            {
              'deleted_at': {
                '$exists': false
              }
            }
          ]
        }
      ]
    }
  },
  {
    '$sort': {
      'created_at': -1
    }
  },
  {
    '$group': {
      '_id': '$msisdn',
      'order': {
        '$last': '$$ROOT'
      }
    }
  },
  {
    '$group': {
      '_id': '$order.status',
      'count': {
        '$sum': 1
      }
    }
  }
]

// output
_id:"ATIVO"
count: 2737
  • 2

    You’re starting to realize why the relational model is still much better for most applications.

  • Capable, always thought that, but, there are situations that we do not have choices :P

1 answer

0


[SOLUTION]

The $sort is losing data before the $group that pulls the last requests.

{
  _id: '$msisdn',
  "order": {
    "$last": "$$ROOT"
  }
}

in my case I just changed position $sort and worked properly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.