0

I would like fetch the records of this year, which is not containing stop state here is my data. Is it possible to achieve by aggregate function?

this is my actual data

    { [
    {
      shortMovie":"name"
       "categrory": "comedy"
      "times": [
        {
          "state": "start",
          "date": "2017-1-09T18:30:00.000Z"
        },
{
          "state": "stop",
          "date": "2017-1-09T19:30:00.000Z"
        }
      ],
      "_id": "58b017bac8966e17dc59bd95"
    }
  ],[
    {
      shortMovie":"name"
       "categrory": "comedy"
      "times": [
        {
          "state": "start",
          "date": "2017-5-09T18:30:00.000Z"
        }
      ],
      "_id": "58b017bac8966e17dc59bd95"
    }
  ],
 [
    {
      shortMovie":"name"
       "categrory": "comedy"
      "times": [
        {
          "state": "start",
          "date": "2017-6-09T18:30:00.000Z"
        },
{
          "state": "pause",
          "date": "2017-6-09T19:30:00.000Z"
        },
{
          "state": "resume",
          "date": "2017-6-09T22:30:00.000Z"
        },
{
          "state": "stop",
          "date": "2017-6-09T23:30:00.000Z"
        }
      ],
      "_id": "58b017bac8966e17dc59bd95"
    }
  ]
}

i tried

const startDate = new Date(data.year, 0, 1),
                endDate = new Date(data.year, 11, 31, 23, 59, 59, 999),
db.collection.find({$and: [{
                    $or: [{
                        "times.0.date": {
                            $gt: startDate,
                            $lt: endDate
                        }
                    }, {
                        "times.date": {
                            $gt: startDate,
                            $lt: endDate
                        }
                    }]
                }]}).toarray(function(result){

                                            result.times.map(function(get_time){
})
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Schüler
  • 512
  • 4
  • 10
  • 25
  • `.find({ "times.state": "stop" })` or even `.find({ "times.state": "stop" }, { "times.$": 1 })` to match the particular array element in the latter case. What's the problem? – Neil Lunn Jun 09 '17 at 12:16
  • sir i need to add two conditions one for current year and finding the entries which does not have `stop`. above i have returned the data which i got from map function – Schüler Jun 09 '17 at 12:24
  • That is not what your question says. [Edit Your Question](https://stackoverflow.com/posts/44457555/edit) please. But really you "should" be reading the duplicate question link answers and learning something. There are more than enough cases presented there. – Neil Lunn Jun 09 '17 at 12:26
  • @NeilLunn yes i have updated please check – Schüler Jun 09 '17 at 12:40
  • Like I said the preference is for you to actually [read the answers already given](https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection). There are examples there with `$filter`, which is the operator you need. – Neil Lunn Jun 09 '17 at 12:45

0 Answers0