node.js - How to get day wise records of particular month in mongodb? -


this schema

empname: {type: string}, soldby: {type: string}, expenseprice:{type:number}, expensedesc:{type:string}, expensetype:{type:string}, createdat: {type: date, default: date.now} 

i tried query

db.expenses.aggregate([     {         $project: {             _id: 1,             year: { $year: "$createdat" },             month: { $month: "$createdat" },             day: { $dayofmonth: "$createdat" },             expenseprice: 1         }     },     {         $group: {            _id: {               year: "$year",               month: "$month",               day: "$day"            },            sum: { $sum: "$expenseprice" }         }     } ]) 

i'm getting output as

{ "_id" : { "year" : 2015, "month" : 8, "day" : 15 }, "sum" : 200 } { "_id" : { "year" : 2016, "month" : 5, "day" : 20 }, "sum" : 150 } { "_id" : { "year" : 2016, "month" : 6, "day" : 29 }, "sum" : 250 } 

i want records of particular year , particular month , in month, day wise this

{ "_id" : { "year" : 2016, "month" : 6, "day" : 28 }, "sum" : 150 } { "_id" : { "year" : 2016, "month" : 6, "day" : 29 }, "sum" : 200 } 

i tried $match too

db.expenses.aggregate([     {         $project: {             _id: 1,             year: { $year: "$createdat" },             month: { $month: "$createdat" },             day: { $dayofmonth: "$createdat" },             expenseprice: 1         }     },     {         $match: {             $eq: ["$month", 06]         }     },     {         $group: {             _id: {                 year: "$year",                 month: "$month",                 day: "$day"             },             sum: { $sum: "$expenseprice" }         }     } ]) 

but i'm getting error this

assert: command failed: {     "ok" : 0,     "errmsg" : "bad query: badvalue unknown top level operator: $eq",     "code" : 16810 } : aggregate failed _geterrorwithcode@src/mongo/shell/utils.js:23:13 doassert@src/mongo/shell/assert.js:13:14 assert.commandworked@src/mongo/shell/assert.js:266:5 dbcollection.prototype.aggregate@src/mongo/shell/collection.js:1215:5 @(shell):1:1  2016-06-29t16:20:47.754+0530 e query    [thread1] error: command failed: {     "ok" : 0,     "errmsg" : "bad query: badvalue unknown top level operator: $eq",     "code" : 16810 } : aggregate failed : _geterrorwithcode@src/mongo/shell/utils.js:23:13 doassert@src/mongo/shell/assert.js:13:14 assert.commandworked@src/mongo/shell/assert.js:266:5 dbcollection.prototype.aggregate@src/mongo/shell/collection.js:1215:5 @(shell):1:1 

please refer below sample how year , month in $group can referred. per understanding, last bit need requirement.

db.expenses.aggregate([    {$project: {_id:1,year:{$year:"$createdat"},month:{$month:"$createdat"}, day:{$dayofmonth:"$createdat"},expenseprice:1}},    {$group: {_id:{year:"$year",month:"$month",day:"$day"}, sum:{$sum:"$expenseprice"}}},    {$match : {"_id.year" : 2016, "_id.month" : 6}}]) 

i not sure why have added group after match. initial understanding, may not required. if above solution not expected, please update comment or requirement. adjust answer accordingly.

i have tested query sample data. data filtered month , day wise breakdown.


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -