Here's a record Customers Collection
{
name: xyz,
.
.
.
createdAt: Sun Nov 20 2016 00:00:00 GMT+0530 (IST)
lastModified: Sat Dec 10 2016 00:00:00 GMT+0530 (IST)
}
I need to be able to get count of customers modified based on date
eg: Date Count(lastModified)
11-20-2016 10
12-20-2016 7
13-20-2016 9
I want the result grouped something like this.
count() method is used to return the count of documents that would match a find() query. The db. collection. count() method does not perform the find() operation but instead counts and returns the number of results that match a query.
Definition. $project. Passes along the documents with the requested fields to the next stage in the pipeline. The specified fields can be existing fields from the input documents or newly computed fields.
if you want to count by lastModified
date and time then can use just like:
db.getCollection('collectionName').aggregate({$group:{_id: "$lastModified", count:{$sum:1}}})
and if you want to count by lastModified
date only then can use just like:
db.getCollection('collectionName').aggregate(
[
{
$group:
{
_id:
{
day: { $dayOfMonth: "$lastModified" },
month: { $month: "$lastModified" },
year: { $year: "$lastModified" }
},
count: { $sum:1 },
date: { $first: "$lastModified" }
}
},
{
$project:
{
date:
{
$dateToString: { format: "%Y-%m-%d", date: "$date" }
},
count: 1,
_id: 0
}
}
])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With