0

i have a document structure like the one mentioned below

    {
    "_id" : ObjectId("asdasdasdasdasdas"),
    "info" : [ 
        {

          "dateTime" : ISODate("2016-08-19T12:10:16.000Z"),
          "srcName" : null,
          "parseFlag" : true,
          "GrId" : null,
          "eventMessage" : "Error1. "
        }, 
       {

          "dateTime" : ISODate("2016-09-19T12:10:16.000Z"),
          "srcName" : null,
          "parseFlag" : false,
          "GrId" : null,
          "eventMessage" : "Error2"
       },
       {

          "dateTime" : ISODate("2016-10-19T12:10:16.000Z"),
          "srcName" : null,
          "parseFlag" : false,
           "GrId" : null,
           "eventMessage" : "Error2"
        },
        ],
    "domain" : "asdasdas",  
    "fileName" : "sdasdsdfsd.dsf.gz",   
    "eventGroup" : "fault",   
    "eventId" : 62,
    "type" : "1"

     }

I have to filter the records based on the datetime in info. For example if i have to get the august month data alone How do i do it?

Output should be something like

   {
    "_id" : ObjectId("asdasdasdasdasdas"),
    "info" : [ 
    {

    "dateTime" : ISODate("2016-08-19T12:10:16.000Z"),
    "srcName" : null,
    "parseFlag" : true,
    "GrId" : null,
    "eventMessage" : "Error1. "
    }, 
    "domain" : "asdasdas",  
    "fileName" : "sdasdsdfsd.dsf.gz",   
    "eventGroup" : "fault",   
    "eventId" : 62,
    "type" : "1"
    }

Using aggregate and unwind takes lot of time. Let me know how to handle this scenario

user3138864
  • 63
  • 1
  • 10
  • For the above example, what time interval did you apply on the embedded `dateTime` field to get the expected output? To me both subdocuments inside the `info` array have the same `dateTime` value so I'm a bit lost on how you got to the above expectation. – chridam May 09 '17 at 07:28
  • That was just for sample i have given the document format. Usually the info will have a list of events with datetime. I have data for almost 3 months. I have to filter the data for one month – user3138864 May 09 '17 at 07:35
  • Can you please update the question with a clear example that we can follow? – chridam May 09 '17 at 07:41
  • Use [`$filter`](https://docs.mongodb.com/manual/reference/operator/aggregation/filter/#exp._S_filter) in the highly upvoted answer as `db.test.aggregate([ { '$project': { 'info': { '$filter': { input: '$info', as: 'item', cond: { $eq: [{'$month': '$$item.dateTime'}, 8]} }} }} ])` – chridam May 09 '17 at 08:40

0 Answers0