0

I have documents with this structure:

{
"_id" : ObjectId("59936474a955fb3f18db07d5"),
"CD_MATRICULA" : 12,
"DT_FROM" : ISODate("2014-04-04T16:21:37.000-03:00"),
"DT_FROM_ANO" : 2012,
"DT_FROM_MES" : 2,
"DT_FROM_DIA" : 2 }

In this sample, DT_FROM_ANO differ from YEAR(DT_FROM), I need to find docs that have this difference, I try this but dont work:

    db.getCollection('TABLESAMPLE').aggregate(
    {$project: {
      CD_MATRICULA: 1,
      DT_FROM: 1, 
      DT_FROM_ANO: 1,
      DT_FROM_MES: 1,
      DT_FROM_DIA: 1,
      DT_TO: 1,
      ANO: {$year: '$DT_FROM'},
      MES: {$month: '$DT_FROM'},
      DIA: {$dayOfMonth: '$DT_FROM'}
    }
  },
  {$match: 
      { DT_FROM_ANO: {$ne: '$ANO'} }
  }
);

All documents will return in this case, exist only one that have YEAR differs. What is wrong ??? I need that only docs with difference between in year returns.

Leonardo
  • 159
  • 1
  • 15
  • 2
    Use `$redact`. `db.getCollection('TABLESAMPLE').aggregate({ $redact: { $cond: [{ $ne: ['$DT_FROM_ANO', {$year: '$DT_FROM'}] }, '$$KEEP', '$$PRUNE' ] } })`. – s7vr Aug 16 '17 at 15:46
  • 2
    Possible duplicate of [MongoDb query condition on comparing 2 fields](https://stackoverflow.com/questions/4442453/mongodb-query-condition-on-comparing-2-fields) – s7vr Aug 16 '17 at 15:46

0 Answers0