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
Post a Comment