MongoDB: Search by date in PHP

0

I have a collection in MongoDB of 45 million records (Actually we had a database in SQL Server, but this is already starting to have problems, so we are moving to Mongo and query the information using PHP. The documents is as follows:

{ 
"_id" : ObjectId("57ed4ec22ca3b409d8d140bf"), 
"fecges" : ISODate("2015-11-16T05:00:00.000+0000"), 
"horges" : "16:28:06", 
"telefono" : "7958279", 
"feccomp" : ISODate("1900-01-01T05:00:00.000+0000"), 
"observacion_gs" : "Esta es una observacion", 
"rango_hora" : "16:00-16:59", 
"inicio_gestion" : ISODate("2015-11-16T21:22:56.000+0000"), 
"fin_gestion" : ISODate("2015-11-16T21:28:06.000+0000"), 
"usuario" : "NOMBRE DE USUARIO", 
"RowID" : NumberLong(1)
}

I need to know how to do the following using PHP:

  • How to consult all the records in the "fecges" field of a specific date (for example, the one shown in this example: 2015-11-16)
  • I need to know which are the records whose "start_gestion" and "fin_gestion" are in the indicated range (eg: start_gestion: 2015-11-16, and fin_gestion: 2015-11-18).

I used the following code, but I get an error that I can not find (syntax error "{"):

$db.r_gestiones.find({ "inicio_gestion": {'$gt': new MongoDate("2015-11-16"), '$gte': new MongoDate("2015-11-16") }});

This code returns too many results:

$db = $conn->r_gestiones;
$coleccion = $db->r_gestiones;
$consulta = array( "inicio_gestion"=>array('$gt'=>new MongoDate(strtotime("2015-11-16 00:00:00"))));
$cursor = $coleccion->find( $consulta );

And the modification of the previous code gives me a lot of information too:

$db = $conn->r_gestiones;
$coleccion = $db->r_gestiones;
$consulta = array( "inicio_gestion"=>array('$gt'=>new MongoDate(strtotime("2015-11-16 00:00:00")), '$gte'=>new MongoDate(strtotime("2015-11-16 23:59:59"))));
$cursor = $coleccion->find( $consulta );
    
asked by Ricky 05.10.2016 в 17:38
source

2 answers

0

Well, I answer my own question:

What happens is that when I return my query too many documents, PHP exceeds the memory limit and sends me to error. What I did to solve this problem and see results was to use "limit" as follows:

$db = $conn->r_gestiones;
$coleccion = $db->r_gestiones;
$consulta = array( "inicio_gestion"=>array('$gt'=>new MongoDate(strtotime("2015-11-16 00:00:00")), '$gte'=>new MongoDate(strtotime("2015-11-16 23:59:59"))));
$cursor = $coleccion->find( $consulta )->limit(10);

In this way I am only getting the first ten records, thus solving my problem. I hope this code is useful for you.

Here is a link to more information about how use "limit"

Thank you.

    
answered by 05.10.2016 / 18:40
source
0

The second operator of your query, $gte , means greater than or equal , then it is almost the same as $gt . Your query returns all records after 23:59 that day. A year and a half of records.

Try the operator $lte (less than or equal) to narrow the results to the records prior to 23:59 on the date you indicate

Your query would look like this:

$consulta = [
    "inicio_gestion"=>[
        '$gte'=>new MongoDate(strtotime("2015-11-16 00:00:00")), 
        '$lte'=>new MongoDate(strtotime("2015-11-16 23:59:59"))
     ]
];

That brings all the tasks started on 2015-11-16.

If you also want to cross it with the works that ended before the end of that day:

$consulta = [
  '$and' => [
    "inicio_gestion"=>[
        '$gte'=>new MongoDate(strtotime("2015-11-16 00:00:00")), 
        '$lte'=>new MongoDate(strtotime("2015-11-16 23:59:59"))
    ],
    "fin_gestion"=>[
        '$lte'=>new MongoDate(strtotime("2015-11-16 23:59:59"))
    ]
  ]
];

In this second case, the operator $lte in the first part of the query is redundant: if ended before midnight on November 16, it is implied that started em> before midnight on November 16.

    
answered by 22.02.2017 в 03:28