MongoDB Aggregation error : Pipeline stage specification object must contain exactly one field

MongoDB is complaining because you have an unrecognised pipeline stage specification "count": { "$sum": 1 } in your pipeline.

Your original pipeline when formatted properly

db.hashtag.aggregate([
    { 
        "$group": {
            "_id": {
                "year": { "$year": "$tweettime" },
                "dayOfYear": { "$dayOfYear": "$tweettime" },
                "interval": {
                    "$subtract": [ 
                        { "$minute": "$tweettime" },
                        { "$mod": [{ "$minute": "$tweettime"}, 15] }
                    ]
                }
            }
        },
        "count": { "$sum": 1 } /* unrecognised pipeline specification here */
    }
])

should have the aggregate accumulator $sum within the $group pipeline as:

    { 
        "$group": {
            "_id": {
                "year": { "$year": "$tweettime" },
                "dayOfYear": { "$dayOfYear": "$tweettime" },
                "interval": {
                    "$subtract": [ 
                        { "$minute": "$tweettime" },
                        { "$mod": [{ "$minute": "$tweettime"}, 15] }
                    ]
                }
            },
            "count": { "$sum": 1 }
        }           
    }
])

Leave a Comment