Wednesday, 24 May 2017

MongoError: can't convert from BSON type missing to Date while Grouping records in nested documents

Basically i want to group polls according to month.

My model :

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var voteSchema = new Schema({
    ip: String,
    votedOn: { type: Date, default: Date.now }
});

var choiceSchema = new Schema({
    text: String,
    votes: [voteSchema]
});

var PollSchema = new Schema({
    question: { type: String, required: true },
    choices: [choiceSchema]
});

module.exports = mongoose.model('Polls', PollSchema);

Sample data :

[
    {
        "_id": "5914056440bfac03711966ad",
        "choices": [
            {
                "text": "c3",
                "_id": "5914056440bfac03711966af",
                "votes": [
                    {
                        "ip": "::1",
                        "_id": "5914058040bfac03711966b4",
                        "votedOn": "2017-05-11T06:32:32.685Z"
                    }
                ]
            },
            {
                "text": "c1",
                "_id": "5914056440bfac03711966ae",
                "votes": [
                    {
                        "ip": "::1",
                        "_id": "587dec8eaccc4b036a193a8f",
                        "votedOn": "2017-01-17T10:06:06.012Z"
                    }
                ]
            }
        ]
    },
    {
        "_id": "5914057540bfac03711966b0",
        "choices": [
            {
                "text": "b3",
                "_id": "5914057540bfac03711966b3",
                "votes": [
                    {
                        "ip": "::1",
                        "_id": "591d6b8caccc4b036a193a8e",
                        "votedOn": "2017-05-18T09:38:20.363Z"
                    }
                ]
            },
            {
                "text": "b2",
                "_id": "5914057540bfac03711966b2",
                "votes": [
                    {
                        "ip": "::1",
                        "_id": "591d69b7accc4b036a193a8d",
                        "votedOn": "2017-05-18T09:30:31.708Z"
                    },
                    {
                        "ip": "::1",
                        "_id": "587dec9aaccc4b036a193a90",
                        "votedOn": "2017-01-17T10:06:18.137Z"
                    }
                ]
            },
            {
                "text": "b1",
                "_id": "5914057540bfac03711966b1",
                "votes": [
                    {
                        "ip": "::1",
                        "_id": "587decafaccc4b036a193a91",
                        "votedOn": "2017-01-17T10:06:39.809Z"
                    }
                ]
            }
        ]
    }
]

But when i try to create group using mongoose something similar to https://stackoverflow.com/a/38596913/5871771

I am getting MongoError: can't convert from BSON type missing to Date error

I have also tried to get basic grouping using following code

Poll.aggregate({ 
            $group : {
                _id : { month: { $month: "$votes.votedOn" }, day: { $dayOfMonth: "$votes.votedOn" }, year: { $year: "$votes.votedOn" } }
            } 
        }, function(err, d) {
            if (err) {
                throw err;
            }
            console.log(JSON.stringify(d, null, 4));
        }
    );

But i am getting the same error



via Rakesh Soni

No comments:

Post a Comment