MongoDB filter an array field by period (start date, end date) using PHP -


i have mongodb documents structure:

{      _id: 1      dates: [          isodate ("2015-08-21t22: 00: 00z")          isodate ("2015-09-27t22: 00: 00z")      ],      ... } 

in example, see document 1 accessed:

  • 2015-08-21
  • 2015-09-27

in application, can filter time period (start date, end date).

if in application filter period "2015-09-01 - 2015-09-01":

logically document 1 should not found because not consulted during period. tests, document 1 found.

what conditions must use filter data correctly?

i tried it's fallible:

<?php $begin = new \datetime('2015-09-01'); $end = new \datetime('2015-09-01');  $expr1 = $qb->expr()->field('dates')->gte($begin); $expr2 = $qb->expr()->field('dates')->lte($end);  $qb     ->addand($expr1)     ->addand($expr2)     ; 

thanks help, yohann

with aggregation

this code snippet mongoshell can try

> db.collection.aggregate([{$unwind:"$dates"},{$match:{"dates":{$gt:new isodate("2015-01-01"),$lt:new isod ate("2015-09-01")}}}]) 

with find query

db.collection.find({dates:{$elemmatch:{$gt:new isodate("2015-01-10"),$lt:new isodate("2015-09-01")}}},{"dates.$":1}) 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -