0

This is my collection in MongoDB

db.time.entries.findOne({"userId" : "k89L3cLE3pEu3Tn2Y"})
{
    "_id" : "eQ9KpemZCSPBzKi7W",
    "createdAt" : ISODate("2016-04-03T21:00:00Z"),
    "userId" : "k89L3cLE3pEu3Tn2Y",
    "duration" : 2700,
    "metadata" : {
            "patientId" : "37253xjB4orjSKPK7",
            "manual" : true,
            "organizationId" : "gb8gbhvhqgYc6x3nK",
            "careManagementType" : "CCM"
    }

}

and When i run the below query i am getting the following error

db.time.entries.aggregate([{$match: {userId: "k89L3cLE3pEu3Tn2Y"}}, 
{ $group : { _id :{ "day" : {"$dayOfYear" : "$createdAt"}, "year": { "$year" : "$createdAt"}}, count: {$sum:"$duration"}}} ])


assert: command failed: {
    "ok" : 0,
    "errmsg" : "can't convert from BSON type EOO to Date",
    "code" : 16006
} : aggregate failed
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:16:14
assert.commandWorked@src/mongo/shell/assert.js:290:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1312:5
@(shell):1:1

Can you please help me in resolving this Problem.I have an ISODate for createdAt Column but I am still getting this error.

  • And how did you get "eQ9KpemZCSPBzKi7W" in the _id field? As far as I know, document IDs are hexadecimal values, this looks really strange. https://docs.mongodb.com/manual/reference/method/ObjectId/ – Laszlo T Jun 19 '17 at 08:31
  • @LaszloTenki It's the default format from meteor, which is in fact what the OP must be using. Or at least the data came from such a source. – Neil Lunn Jun 19 '17 at 09:02
  • What it means is that in fact some of your `"createdAt"` fields are "empty" and therefore not a BSON Date object. To fix this, change your `$match` pipeline to exclude them `{ "$match": { "userId": "k89L3cLE3pEu3Tn2Y" , "createdAt": { "$type": 9 } } }`. Then queries to documents with an empty field will not return and throw the error. Given the `$group` stage, there would be little point including such documents and giving them a "default" date via `$ifNull`. But that would be the other alternative. – Neil Lunn Jun 19 '17 at 09:07

0 Answers0