I have a schema with each website name appear in many collection.I tried to count the number of each website but i couldn't. Can you help me?

const articleSchema = new mongoose.Schema(
    _id: Number,
    link: String,
    title: String,
    sapo: String,
    publicDate: Date,
    sourceCode: String,
    text: String,
    thumbnail: String,
    tags: [String],
    category: { id: Number, name: String },
    website: { id: Number, name: String },

1 Answers

Vikash Singh On Best Solutions

You can do this in 2 ways. One with aggregate query and second with distinct and count query. See both the approaches below and try them in mongodb shell:

Approach 1:

        $group: {
            _id: "$website.name"
            _id: null,
            count: {$sum: 1}
        $project: {
            _id: 0

Approach 2: