0

this is the Structure of the collection

{
"userid": "c7ff591eda700a6d159fe74677f02c98",
"__v": 0,
"created": "2017-02-02T09:35:55.372Z",
"schedules": [
  {
    "description": "demo event",
    "scheduled": "2/1/17 3:05 PM"
  },
  {
    "scheduled": "1/25/17 3:06 PM",
    "description": "demo event 2"
  },
  {
    "scheduled": "2/4/17 3:06 PM",
    "description": "demo event 3"
  },
  {
    "scheduled": "2/1/17 3:10 PM",
    "description": "demo"
  },
  {
    "scheduled": "2/9/17 3:10 PM",
    "description": "demo2"
  },
  {
    "scheduled": "2/1/17 3:23 PM",
    "description": "das"
  },
  {
    "scheduled": "2/3/16 3:30 PM",
    "description": "call ted"
  },
  {
    "scheduled": "2/3/16 3:30 PM",
    "description": "call ted"
  },
  {
    "scheduled": "2/3/16 3:30 PM",
    "description": "call ted"
  }
]

}

I just want to get the data which are scheduled after current date and time.

i have also tried

  db.<collection name>.find({"userid":c7ff591eda700a6d159fe74677f02c98},
          {schedules:{$elemMatch:{scheduled:{$gte:'2/1/17 4:00 PM'}}}});

this statement return's only return scheduled just after that 1 feb that is "demo event3".

      db.<collection name>.find({"userid":c7ff591eda700a6d159fe74677f02c98,
         'schedules.scheduled':{$gte:'2/1/17 4:00 PM'});

this query return's whole collection. some one please help me..

rohanraj
  • 392
  • 2
  • 12

1 Answers1

1

when we use the below query it jumps onto another document in the collection after finding the match in the current document.

  db.<collection name>.find({"userid":c7ff591eda700a6d159fe74677f02c98},
      {schedules:{$elemMatch:{scheduled:{$gte:'2/1/17 4:00 PM'}}}});

to solve this issue we can use

       var date = '2/1/17 3:40 PM';
      db.<collection_name>.aggregate([
       {'$match':{'schedules':{'$elemMatch': 
         {"scheduled":{'$gte': date}}}}},
           {'$unwind':'$schedules'},
             {'$match':{"schedules.scheduled":{"$gte":date}}},
                {'$group':{'_id':"$_id",'schedules':
                  {'$addToSet':'$schedules'}}}])

it will return all the schedules which are in future from the given date string.

rohanraj
  • 392
  • 2
  • 12