Vsevolod Fedorov
Vsevolod Fedorov

Reputation: 521

Can't select several rows from mongo database

I want to aggregate some rows from my mongo database. I have this table in the picture. started table Sample this database:

[ { _id: 5c2857dbf45028a6280078d8,
    sum: 123,
    currency: 'UAH',
    source: 'Видатки',
    description: 'Сходив по хліба',
    date: '2018-12-16' },
  { _id: 5c286449353de8149c1a0785,
    sum: 101,
    currency: 'EUR',
    source: 'Прибутки',
    date: '2018-12-17',
    description: '-' },
  { _id: 5c287d0d51dcdf61f9cda68e,
    sum: 14,
    currency: 'UAH',
    type: 'Видатки',
    description: 'Сходив по хліба 2',
    date: '2018-12-16',
    source: '-' },
  { _id: 5c287d0d51dcdf61f9cda68f,
    sum: 16,
    currency: 'EUR',
    type: 'Прибутки',
    date: '2018-12-17',
    description: 'Транспорт',
    source: 'Видатки' },
  { _id: 5c324f2a1596d0471c379fa6,
    sum: '200',
    currency: 'USD',
    source: 'Видатки',
    description: 'Ремонт',
    date: '2019-01-01' } 
]

I trying to change $or expression on $and, but its not working

db.collection('operations').aggregate([
   { 
      $match: { $or: [ { $and: [ {sum: { $gt: 100}}, {currency: {$eq: "EUR"} } ]  }, { $and: [ {sum: { $gt: 150}}, {currency: {$eq: "USD"} } ] } ] }
   }
], callback);

I expected to select operations where i have sum > 100 in EUR currency and rows with USD with sum > 150. But i have only rows with sum > 100 in EUR. what i have after

Upvotes: 0

Views: 51

Answers (1)

right
right

Reputation: 252

Bro,

Your sum is string

_id: 5c324f2a1596d0471c379fa6, sum: '200', currency: 'USD', source: 'Видатки', description: 'Ремонт', date: '2019-01-01' }

Upvotes: 1

Related Questions