You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MongoDB中如何统计images数组含多个jpg类型的文档数量

MongoDB统计含多个指定类型图片的文档总数

需求描述

需要统计images数组中包含**超过1个type为'jpg'**的图片对象的文档总数,针对给定数据集,预期结果为2(对应_id为1和3的文档)。

数据集示例

[
    {
        _id: ObjectId("1"),
        name: "John Doe",
        age: 25,
        address: {
            street: "123 Main St",
            city: "New York",
            state: "NY",
            zip: "10001"
        },
        images : [
            {
                url: "http://www.example.com/image1.jpg",
                caption: "Image 1",
                type: "jpg"
            },
            {
                url: "http://www.example.com/image2.jpg",
                caption: "Image 2",
                type: "png"
            },
            {
                url: "http://www.example.com/image3.jpg",
                caption: "Image 3",
                type: "svg"
            },
            {
                url: "http://www.example.com/image4.jpg",
                caption: "Image 4",
                type: "jpg"
            }
        ]
    },
    {
        _id: ObjectId("2"),
        name: "Jack",
        age: 22,
        address: {
            street: "132 lake",
            city: "New York",
            state: "NY",
            zip: "10001"
        },
        images : [
            {
                url: "http://www.example.com/image212.jpg",
                caption: "Image 212",
                type: "png"
            },
            {
                url: "http://www.example.com/image34.jpg",
                caption: "Image 34",
                type: "svg"
            }
        ]
    },
    {
         _id: ObjectId("3"),
        name: "Random name",
        age: 27,
        address: {
            street: "Lajpat nagar",
            city: "New Delhi",
            state: "Delhi",
            zip: "121002"
        },
        images : [
            {
                url: "http://www.example.com/image5.jpg",
                caption: "Image 5",
                type: "jpg"
            },
            {
                url: "http://www.example.com/image7.jpg",
                caption: "Image 7",
                type: "png"
            },
            {
                url: "http://www.example.com/image12.jpg",
                caption: "Image 12",
                type: "svg"
            },
            {
                url: "http://www.example.com/image8.jpg",
                caption: "Image 8",
                type: "jpg"
            }
        ]
    }
]

问题

尝试使用$size、$unwind、$group操作符但未成功,请问如何编写MongoDB查询语句?


解决方案

方法一:$filter+$size聚合管道(性能更优)

无需展开数组,直接在文档内计算符合条件的元素数量,避免大量数据展开带来的性能损耗:

db.collection.aggregate([
    // 计算每个文档中type为jpg的图片数量
    {
        $addFields: {
            jpgCount: {
                $size: {
                    $filter: {
                        input: "$images",
                        cond: { $eq: ["$$this.type", "jpg"] }
                    }
                }
            }
        }
    },
    // 筛选出jpg数量大于1的文档
    {
        $match: {
            jpgCount: { $gt: 1 }
        }
    },
    // 统计符合条件的文档总数
    {
        $count: "totalDocuments"
    }
])

方法二:$unwind+$group聚合管道

如果需要基于展开数组的方式实现,可按以下步骤编写:

db.collection.aggregate([
    // 展开images数组
    { $unwind: "$images" },
    // 筛选出type为jpg的元素
    { $match: { "images.type": "jpg" } },
    // 按文档_id分组,统计每个文档的jpg数量
    {
        $group: {
            _id: "$_id",
            jpgCount: { $sum: 1 }
        }
    },
    // 筛选出jpg数量大于1的分组
    { $match: { jpgCount: { $gt: 1 } } },
    // 统计符合条件的分组总数(即目标文档数)
    { $count: "totalDocuments" }
])

结果说明

两种方法均会返回预期结果:

{ "totalDocuments" : 2 }

内容的提问来源于stack exchange,提问作者matrixman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 05:15:36