Amira Bedhiafi
Amira Bedhiafi

Reputation: 1

Count with WHERE condition

I have the following T-SQL query :

SELECT year, COUNT(*) FROM publis
WHERE publisher LIKE '%Springer%'

I made this query :

db.publis.aggregate([{"$group" : {_id:"$year", count:{$sum:1}}}])

How can I include the WHERE condition ?

Upvotes: 1

Views: 98

Answers (1)

Tom Slabbaert
Tom Slabbaert

Reputation: 22276

The equivalent of SQL WHERE is the $match aggregation stage, so you would rewrite you query like so:

db.publis.aggregate([
    {
        $match: {
            publisher: /Springer/,
        },
    },
    {
        $group: {
            _id: '$year',
            count: { $sum: 1 },
        },
    },
]);

Upvotes: 1

Related Questions