1

here expample of my data:

 '_id' => new MongoId("54087e076c03943c3c8b456b"),
        'fornitureFuture' => 
      array (
        '0' => 
        array (
          'data_start'▼ => new MongoDate(1412114400, 0),
          'data_end' => new MongoDate(1414710000, 0),
          'f1' => '65',
          'f2' => new MongoInt32(0),
          'f3' => '45',
          'fornitore' => new MongoId("5346cb2ab9d6f0021e6b18a0"),
        ),
        '1' => 
        array (
          'data_start' => new MongoDate(1420066800, 0),
          'data_end' => new MongoDate(1427752800, 0),
          'f1' => '63.75',
          'f2' => new MongoInt32(0),
          'f3' => '70.4',
          'fornitore' => new MongoId("533406896c0394a62c8b4569"),
        ),

i need to find if exist a data in fornitureFuture with my MongoDate between data_start and data_end ...

first group data_start is 10/01/2014 and data_end 10/31/2014
Second group data_start is 01/01/2015 and data_end 03/31/2015

Something like:

//today is 09/19/2014 

$dataTest = mktime(0,0,0,date('n')+2,14,date('Y'));   //return 11/14/2014

$testMese = $this->db->getOne('MyCollection', array('_id'=> new \MongoId($thisPodPdr['_id']), 'fornitureFuture.data_start'=>array('$lte'=> new \MongoDate($dataTest)) , 'fornitureFuture.data_end'=>array('$gt'=> new \MongoDate($dataTest))   ) , array('fornitureFuture'=>1) );

I expect empty response but return the record..

2 example:

$dataTest = mktime(0,0,0,date('n')+7,14,date('Y'));   //return 04/14/2015

$testMese = $this->db->getOne('MyCollection', array('_id'=> new \MongoId($thisPodPdr['_id']), 'fornitureFuture.data_start'=>array('$lte'=> new \MongoDate($dataTest)) , 'fornitureFuture.data_end'=>array('$gt'=> new \MongoDate($dataTest))   ) , array('fornitureFuture'=>1) );

return correctly empty!

I need to test in the same block ...something like

'fornitureFuture.$.data_start'=>array('$lte'=> new \MongoDate($dataTest)) , 'fornitureFuture.$.data_end'=>array('$gt'=> new \MongoDate($dataTest))

but dont work .

the \ is from namespace and this->db->getOne(collection,$query,$fields) is my function like $this->collection->findOne($query,$fields);

No syntax error.

sorry for my english and thanks for the help

1 Answer 1

1

Your first example, which returns 2 results when you expect none, runs the following query:

[
  '_id' => new MongoId(...),
  'fornitureFuture.data_start' => ['$lte' => new MongoDate(1418533200)],
  'fornitureFuture.data_end' => ['$gt' => new MongoDate(1418533200)],
]

The example document you provided has two array elements, with the following date ranges:

  1. 1412114400 to 1414710000
  2. 1420066800 to 1427752800

This document matches because 1412114400 (of the first element) is less than 1418533200, and 1427752800 (of the second element) is greater than 1418533200. By simply referring to fornitureFuture.data_start and fornitureFuture.data_end, MongoDB's query matcher will be satisfied if any array element's sub-field meets the criteria.

You likely want to restrict start/end criteria to same array element, in which case $elemMatch is what you're looking for:

[
  '_id' => new MongoId(...),
  'fornitureFuture' => [
    '$elemMatch' => [
        'data_start' => ['$lte' => new MongoDate(1418533200)],
        'data_end' => ['$gt' => new MongoDate(1418533200)],
    ],
  ],
]

This criteria should now match only when the start/end dates of the same element satisfy the range. On a related note, you may also be interested in the $ projection operator, to limit fornitureFuture to only the matched element(s).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.