Kant's group with a unit in Mongodbe

I am trying to combine and group objects in mongodb by month. I am basically copying the request from the mongo docs.

db.users.aggregate(
    { 
        $group : {
            _id: {
                month : { $month : "$registrationDate" }
            },
            count: { $sum: 1 }
        }
    }
);

      

Registration type Date - date. A short version of an object in the user collection.

{
    "_id" : ObjectId("50ab08399b57f2be03000000"),
    ...
    "registrationDate" : ISODate("2012-11-20T05:34:01.000Z"),
    ...
}

      

Then I get an exception

exception: can't convert from BSON type NumberDouble to Date

      

+3


source to share


1 answer


The problem is that you have some documents in your collection where the type is registrationDate

not a date but a double precision floating point number. These documents can be found using db.users.find( { registrationDate: { $type:1 } } )

. Correct these documents and they should work. Alternatively, you can add the following step to the start of your aggregation to exclude those documents where registrationDate is not a date:{$match: { registrationDate: { $type:9 } } }



+1


source







All Articles