1

I have a collection like the described below.

When I apply the following filter:

db.getCollection("Marcaciones")
  .find(
    {
      uuid: "12345",
      fecha_registro: { $gte: ISODate("2019-06-20 00:00:00.000Z") },
      fecha_registro: { $lte: ISODate("2019-06-25 23:59:59.999Z") }
    },
    { fecha_registro: 1, _id: 0 }
  )
  .sort({ fecha_registro: -1 });

but I do not know how to group the dates and / or something similar to the distinct in MongoDB

I want to get this result:

{"fecha_registro": "2019-06-20"},
{"fecha_registro": "2019-06-21"},
{"fecha_registro": "2019-06-22"},
{"fecha_registro": "2019-06-24"},
{"fecha_registro": "2019-06-25"}

Using below dataset;

db.Marcaciones.insert({ "uuid": "12345", "tipo": "1", "fecha_registro": ISODate("2019-06-19 09:11:45.285Z") })
db.Marcaciones.insert({ "uuid": "12345", "tipo": "2", "fecha_registro": ISODate("2019-06-19 12:22:22.665Z") })
db.Marcaciones.insert({ "uuid": "12345", "tipo": "3", "fecha_registro": ISODate("2019-06-19 12:54:55.788Z") })
db.Marcaciones.insert({ "uuid": "12345", "tipo": "4", "fecha_registro": ISODate("2019-06-19 18:07:10.138Z") })

db.Marcaciones.insert({ "uuid": "12345", "tipo": "1", "fecha_registro": ISODate("2019-06-20 08:47:12.982Z") })
db.Marcaciones.insert({ "uuid": "12345", "tipo": "2", "fecha_registro": ISODate("2019-06-20 12:23:24.866Z") })
db.Marcaciones.insert({ "uuid": "12345", "tipo": "3", "fecha_registro": ISODate("2019-06-20 13:00:28.387Z") })
db.Marcaciones.insert({ "uuid": "12345", "tipo": "4", "fecha_registro": ISODate("2019-06-20 17:59:57.922Z") })

db.Marcaciones.insert({ "uuid": "12345", "tipo": "1", "fecha_registro": ISODate("2019-06-21 08:51:17.527Z") })
db.Marcaciones.insert({ "uuid": "12345", "tipo": "2", "fecha_registro": ISODate("2019-06-21 12:20:23.028Z") })
db.Marcaciones.insert({ "uuid": "12345", "tipo": "3", "fecha_registro": ISODate("2019-06-21 12:46:06.670Z") })

db.Marcaciones.insert({ "uuid": "12345", "tipo": "1", "fecha_registro": ISODate("2019-06-22 09:06:18.442Z") })
db.Marcaciones.insert({ "uuid": "12345", "tipo": "2", "fecha_registro": ISODate("2019-06-22 13:24:17.891Z") })
db.Marcaciones.insert({ "uuid": "12345", "tipo": "3", "fecha_registro": ISODate("2019-06-22 13:31:00.606Z") })
db.Marcaciones.insert({ "uuid": "12345", "tipo": "4", "fecha_registro": ISODate("2019-06-22 13:31:05.537Z") })

db.Marcaciones.insert({ "uuid": "12345", "tipo": "1", "fecha_registro": ISODate("2019-06-24 08:51:21.862Z") })
db.Marcaciones.insert({ "uuid": "12345", "tipo": "2", "fecha_registro": ISODate("2019-06-24 12:28:49.500Z") })
db.Marcaciones.insert({ "uuid": "12345", "tipo": "3", "fecha_registro": ISODate("2019-06-24 13:08:36.422Z") })
db.Marcaciones.insert({ "uuid": "12345", "tipo": "4", "fecha_registro": ISODate("2019-06-24 18:00:41.499Z") })

db.Marcaciones.insert({ "uuid": "12345", "tipo": "1", "fecha_registro": ISODate("2019-06-25 08:49:03.776Z") })
db.Marcaciones.insert({ "uuid": "12345", "tipo": "2", "fecha_registro": ISODate("2019-06-25 13:27:05.152Z") })
db.Marcaciones.insert({ "uuid": "12345", "tipo": "3", "fecha_registro": ISODate("2019-06-25 13:44:02.609Z") })

db.Marcaciones.insert({ "uuid": "12345", "tipo": "1", "fecha_registro": ISODate("2019-06-26 08:49:06.356Z") })
db.Marcaciones.insert({ "uuid": "12345", "tipo": "2", "fecha_registro": ISODate("2019-06-26 12:30:52.041Z") })
db.Marcaciones.insert({ "uuid": "12345", "tipo": "3", "fecha_registro": ISODate("2019-06-26 13:18:37.833Z") })
db.Marcaciones.insert({ "uuid": "12345", "tipo": "4", "fecha_registro": ISODate("2019-06-26 18:02:35.837Z") })

db.Marcaciones.insert({ "uuid": "12345", "tipo": "1", "fecha_registro": ISODate("2019-06-27 08:43:45.077Z") })
db.Marcaciones.insert({ "uuid": "12345", "tipo": "2", "fecha_registro": ISODate("2019-06-27 12:33:16.952Z") })
buræquete
  • 14,226
  • 4
  • 44
  • 89

1 Answers1

1

You need to utilize an aggregate array;

Using version 4.0.6 MongoDB

db.Marcaciones.aggregate(
    [ 
        { $match : { $and : 
                [
                    { uuid : "12345" },
                    { fecha_registro:  {"$gte": ISODate("2019-06-20 00:00:00.000Z")}},
                    { fecha_registro:  {"$lte": ISODate("2019-06-25 23:59:59.999Z")}}
                ]
            } 
        },
        { $project: { fecha_registro: { $dateToString: { format: "%Y-%m-%d", date: "$fecha_registro" } }, _id: 0 } },
        { $group: { _id: '$fecha_registro' } },
        { $sort : { _id : 1 } }
    ]
);

I was able to get the following with above query;

{"_id": "2019-06-20"},
{"_id": "2019-06-21"},
{"_id": "2019-06-22"},
{"_id": "2019-06-24"},
{"_id": "2019-06-25"}

Important parts were $dateToString, and $group


If you want fecha_registro as the name in the final result rather than _id, you can add;

{ $project: { fecha_registro: "$_id", _id: 0 } }

at the end of the aggregation.

ps. your expected data is wrong "2019-06-23" shouldn't be in the result. Since there is no such date in your collection data...

buræquete
  • 14,226
  • 4
  • 44
  • 89