I have a collection with various documents but i am trying to match and group only a certain subset of documents that match a certain condition:
scanned: true
{ "_id" : "uqtkoew8fxkqhif7t2im1geb", "_class" : "mz.co.crunchtech.vtchill.domain.tickets.PrivateWithTablesTicket", "tableId" : "59bba43c77c82a7d95b2bef2", "scanNumber" : 2, "maxScans" : 2, "scanDate" : ISODate("2017-09-18T19:36:37.045Z"), "creationDate" : ISODate("2017-09-15T11:37:20.411Z"), "type" : "pwtbday", "numberOfPeople" : 2, "eventId" : "59bba42d77c82a7d95b2bef1", "client" : { "email" : "", "phoneNumber" : "258849901374", "name" : "Rui Cossa" }, "smsState" : false, "rsvp" : 0, "scanned" : true }
{ "_id" : "wklyuxdkljjccoqymc5xee58", "_class" : "mz.co.crunchtech.vtchill.domain.tickets.PrivateWithTablesTicket", "tableId" : "None", "scanNumber" : 1, "maxScans" : 1, "scanDate" : ISODate("2017-09-21T20:14:42.383Z"), "creationDate" : ISODate("2017-09-18T23:17:47.681Z"), "type" : "pwtbday", "numberOfPeople" : 1, "eventId" : "59bba42d77c82a7d95b2bef1", "client" : { "email" : "", "phoneNumber" : "", "name" : "Gildo zefanias" }, "smsState" : false, "rsvp" : 0, "scanned" : true }
{ "_id" : "fu6dblxi3zcotnxtqhiqik28", "_class" : "mz.co.crunchtech.vtchill.domain.tickets.PrivateWithTablesTicket", "tableId" : "None", "scanNumber" : 0, "maxScans" : 1, "creationDate" : ISODate("2017-09-18T23:17:47.988Z"), "type" : "pwtbday", "numberOfPeople" : 1, "eventId" : "59bba42d77c82a7d95b2bef1", "client" : { "email" : "", "phoneNumber" : "", "name" : "Rogerio Zandamela" }, "smsState" : false, "rsvp" : 0, "scanned" : true }
{ "_id" : "2veawo9d0vya7jegdiuv5yl0", "_class" : "mz.co.crunchtech.vtchill.domain.tickets.PrivateWithTablesTicket", "tableId" : "None", "scanNumber" : 1, "maxScans" : 1, "scanDate" : ISODate("2017-09-21T20:14:42.383Z"), "creationDate" : ISODate("2017-09-18T23:17:48.015Z"), "type" : "pwtbday", "numberOfPeople" : 1, "eventId" : "59bba42d77c82a7d95b2bef1", "client" : { "email" : "", "phoneNumber" : "", "name" : "Yuka" }, "smsState" : false, "rsvp" : 0, "scanned" : true }
Even though all of the documents above have a scanDate the aggregation below returns an error:
db.ticket.aggregate(
[ {$match: {"scanned": true}},
{
$group : {
_id : { day: {$dayOfMonth: "$scanDate"},hour: {$hour: "$scanDate"}},
totalScans: { $sum: "$scanNumber"},
count: { $sum: 1 }
}
}
]
);
ERROR:
assert: command failed: { "ok" : 0, "errmsg" : "can't convert from BSON type null to Date", "code" : 16006, "codeName" : "Location16006" } : aggregate failed
How to limit the aggregation only to documents that match the condition of the match portion