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 );