Reputation: 1291
I have the following query
collection.aggregate([
{"$match": {"timestamp": {"$gte": lastDateInUnix}} },
{ "$group": {
"_id": {
"$dateToString": {
"format": "%d-%m-%Y",
"date": {
"$toDate": {
"$multiply": [1000, "$timestamp"]
}
}
}
},
"totalMessages": {"$sum": 1 }
}
}
])
This returns something like this:
[
{
"_id": "05-01-2020",
"totalMessages": 4
}
]
As "_id" is a required field, is it possible to rename "_id" to say "dateOccurred"?
Upvotes: 1
Views: 73
Reputation: 17915
You use a $project
to do that, _id
is a required field for documents existing in DB, but you transform the document or result the way you like using .aggregate()
or .find()
which are two ways you retrieve data from DB :
collection.aggregate([
{ "$match": { "timestamp": { "$gte": lastDateInUnix } } },
{
"$group": {
"_id": {
"$dateToString": {
"format": "%d-%m-%Y",
"date": {
"$toDate": {
"$multiply": [1000, "$timestamp"]
}
}
}
},
"totalMessages": { "$sum": 1 }
}
},
{ $project: { dateOccurred: '$_id', totalMessages: 1, _id: 0 } }
])
Upvotes: 1