sql >> Base de Datos >  >> NoSQL >> MongoDB

MongoDB encuentra los registros de hoy

podemos usar $where

db.collection.find(
   { $where: "this._id.getTimestamp() >= ISODate('2017-02-25')" }
)

Para obtener documentos de hoy, o mejor dicho de la medianoche pasada:

db.collection.find( { $where: function() { 
    today = new Date(); //
    today.setHours(0,0,0,0);
    return (this._id.getTimestamp() >= today)
} } );

por supuesto, es mucho más rápido tener un campo de marca de tiempo indexado o seguir el enfoque con el cálculo de un ObjectID para la fecha de inicio y comparar _id con él, ya que _id también está indexado.