zhwITbird 2021-05-24 13:30 采纳率: 100%
浏览 46
已采纳

mongo 数组分页并返回数组中指定的字段

    刚接触mongo,希望mongo大佬解惑,感谢。

需求

    数据结构中有有个数组`malIps`,这个数组中有个字段`attack`,想对malIps这个数组进行分页,并且不返回`attack`这个数组对象。

mongo中存储的数据结构:

{
        "_id" : ObjectId("60ab1cd22119d970838a7db8"),
        "attackTypeAmount" : 7,
        "logTotal" : 48500,
        "attackTotal" : 9758,
        "attackTypeTotal" : {
                "scanrobots" : 330,
                "FileInclude" : 4020,
                "Javaouinstream" : 6,
                "Sensitivefile" : 4785,
                "SQLInjection" : 6,
                "Webshell" : 12,
                "XSS" : 599
        },
        "malIpTotal" : 3,
        "malIps" : [
                {
                        "attackCounts" : 9758,
                        "telecomOperator" : "",
                        "finishTime" : "2020-12-03 16:05:46",
                        "isMalIp" : false,
                        "attackTypeAmount" : 9758,
                        "ip" : "192.168.0.112",
                        "latitude" : "",
                        "location" : "",
                        "startTime" : "2020-12-02 13:55:59",
                        "attackTypeTotal" : {
                                "scanrobots" : 330,
                                "FileInclude" : 4020,
                                "Javaouinstream" : 6,
                                "Sensitivefile" : 4785,
                                "SQLInjection" : 6,
                                "Webshell" : 12,
                                "XSS" : 599
                        },
                        "longitude" : "",
                        "attack" : [
                                {
                                        "dateTime" : "2020-12-03 16:05:46",
                                        "lineNo" : 48224,
                                        "log" : "192.168.0.112 - - [03/Dec/2020:16:05:46  0800] \"GET /noindex/common/css//../../../WEB-INF/web.xml\u0000.jsp HTTP/1.1\" 404 242 \"-\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.21 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.21\"",
                                        "filePath" : "F:\\test\\logs\\apache\\access_log",
                                        "type" : "Sensitivefile",
                                        "status" : "404"
                                },
                                {
                                        "dateTime" : "2020-12-03 16:05:46",
                                        "lineNo" : 48272,
                                        "log" : "192.168.0.112 - - [03/Dec/2020:16:05:46  0800] \"GET /noindex/common/fonts/documents HTTP/1.1\" 404 228 \"-\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.21 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.21\"",
                                        "filePath" : "F:\\test\\logs\\apache\\access_log",
                                        "type" : "XSS",
                                        "status" : "404"
                                }
                        ]
                },
                {
                        "attackCounts" : 9758,
                        "telecomOperator" : "",
                        "finishTime" : "2020-12-03 16:05:46",
                        "isMalIp" : false,
                        "attackTypeAmount" : 9758,
                        "ip" : "192.168.0.113",
                        "latitude" : "",
                        "location" : "",
                        "startTime" : "2020-12-02 13:55:59",
                        "attackTypeTotal" : {
                                "scanrobots" : 330,
                                "FileInclude" : 4020,
                                "Javaouinstream" : 6,
                                "Sensitivefile" : 4785,
                                "SQLInjection" : 6,
                                "Webshell" : 12,
                                "XSS" : 599
                        },
                        "longitude" : "",
                        "attack" : [
                                {
                                        "dateTime" : "2020-12-03 16:05:46",
                                        "lineNo" : 48224,
                                        "log" : "192.168.0.113 - - [03/Dec/2020:16:05:46  0800] \"GET /noindex/common/css//../../../WEB-INF/web.xml\u0000.jsp HTTP/1.1\" 404 242 \"-\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.21 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.21\"",
                                        "filePath" : "F:\\test\\logs\\apache\\access_log",
                                        "type" : "Sensitivefile",
                                        "status" : "404"
                                },
                                {
                                        "dateTime" : "2020-12-03 16:05:46",
                                        "lineNo" : 48300,
                                        "log" : "192.168.0.113 - - [03/Dec/2020:16:05:46  0800] \"GET /noindex/common/images/documents HTTP/1.1\" 404 229 \"-\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.21 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.21\"",
                                        "filePath" : "F:\\test\\logs\\apache\\access_log",
                                        "type" : "XSS",
                                        "status" : "404"
                                }
                        ]
                },
                {
                        "attackCounts" : 9758,
                        "telecomOperator" : "",
                        "finishTime" : "2020-12-03 16:05:46",
                        "isMalIp" : false,
                        "attackTypeAmount" : 9758,
                        "ip" : "192.168.0.114",
                        "latitude" : "",
                        "location" : "",
                        "startTime" : "2020-12-02 13:55:59",
                        "attackTypeTotal" : {
                                "scanrobots" : 330,
                                "FileInclude" : 4020,
                                "Javaouinstream" : 6,
                                "Sensitivefile" : 4785,
                                "SQLInjection" : 6,
                                "Webshell" : 12,
                                "XSS" : 599
                        },
                        "longitude" : "",
                        "attack" : [ ]
                }
        ]
}

    想得到的数据结构,得到`malIps`的前两条数据,不要`attack`字段,如下:

{
        "_id" : ObjectId("60ab1cd22119d970838a7db8"),
        "attackTypeAmount" : 7,
        "logTotal" : 48500,
        "attackTotal" : 9758,
        "attackTypeTotal" : {
                "scanrobots" : 330,
                "FileInclude" : 4020,
                "Javaouinstream" : 6,
                "Sensitivefile" : 4785,
                "SQLInjection" : 6,
                "Webshell" : 12,
                "XSS" : 599
        },
        "malIpTotal" : 3,
        "malIps" : [
                {
                        "attackCounts" : 9758,
                        "telecomOperator" : "",
                        "finishTime" : "2020-12-03 16:05:46",
                        "isMalIp" : false,
                        "attackTypeAmount" : 9758,
                        "ip" : "192.168.0.112",
                        "latitude" : "",
                        "location" : "",
                        "startTime" : "2020-12-02 13:55:59",
                        "attackTypeTotal" : {
                                "scanrobots" : 330,
                                "FileInclude" : 4020,
                                "Javaouinstream" : 6,
                                "Sensitivefile" : 4785,
                                "SQLInjection" : 6,
                                "Webshell" : 12,
                                "XSS" : 599
                        },
                        "longitude" : ""
                },
                {
                        "attackCounts" : 9758,
                        "telecomOperator" : "",
                        "finishTime" : "2020-12-03 16:05:46",
                        "isMalIp" : false,
                        "attackTypeAmount" : 9758,
                        "ip" : "192.168.0.113",
                        "latitude" : "",
                        "location" : "",
                        "startTime" : "2020-12-02 13:55:59",
                        "attackTypeTotal" : {
                                "scanrobots" : 330,
                                "FileInclude" : 4020,
                                "Javaouinstream" : 6,
                                "Sensitivefile" : 4785,
                                "SQLInjection" : 6,
                                "Webshell" : 12,
                                "XSS" : 599
                        },
                        "longitude" : ""
                }
        ]
}

     测试用数据插入:

insert({"attackTypeAmount":7,"logTotal":48500,"attackTotal":9758,"attackTypeTotal":{"scanrobots":330,"FileInclude":4020,"Javaouinstream":6,"Sensitivefile":4785,"SQLInjection":6,"Webshell":12,"XSS":599},"malIpTotal":3,"malIps":[{"attackCounts":9758,"telecomOperator":"","finishTime":"2020-12-03 16:05:46","isMalIp":false,"attackTypeAmount":9758,"ip":"192.168.0.112","latitude":"","location":"","startTime":"2020-12-02 13:55:59","attackTypeTotal":{"scanrobots":330,"FileInclude":4020,"Javaouinstream":6,"Sensitivefile":4785,"SQLInjection":6,"Webshell":12,"XSS":599},"longitude":"","attack":[{"dateTime":"2020-12-03 16:05:46","lineNo":48224,"log":"192.168.0.112 - - [03/Dec/2020:16:05:46  0800] \"GET /noindex/common/css//../../../WEB-INF/web.xml\u0000.jsp HTTP/1.1\" 404 242 \"-\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.21 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.21\"","filePath":"F:\\test\\logs\\apache\\access_log","type":"Sensitivefile","status":"404"},{"dateTime":"2020-12-03 16:05:46","lineNo":48272,"log":"192.168.0.112 - - [03/Dec/2020:16:05:46  0800] \"GET /noindex/common/fonts/documents HTTP/1.1\" 404 228 \"-\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.21 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.21\"","filePath":"F:\\test\\logs\\apache\\access_log","type":"XSS","status":"404"}]},{"attackCounts":9758,"telecomOperator":"","finishTime":"2020-12-03 16:05:46","isMalIp":false,"attackTypeAmount":9758,"ip":"192.168.0.113","latitude":"","location":"","startTime":"2020-12-02 13:55:59","attackTypeTotal":{"scanrobots":330,"FileInclude":4020,"Javaouinstream":6,"Sensitivefile":4785,"SQLInjection":6,"Webshell":12,"XSS":599},"longitude":"","attack":[{"dateTime":"2020-12-03 16:05:46","lineNo":48224,"log":"192.168.0.113 - - [03/Dec/2020:16:05:46  0800] \"GET /noindex/common/css//../../../WEB-INF/web.xml\u0000.jsp HTTP/1.1\" 404 242 \"-\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.21 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.21\"","filePath":"F:\\test\\logs\\apache\\access_log","type":"Sensitivefile","status":"404"},{"dateTime":"2020-12-03 16:05:46","lineNo":48300,"log":"192.168.0.113 - - [03/Dec/2020:16:05:46  0800] \"GET /noindex/common/images/documents HTTP/1.1\" 404 229 \"-\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.21 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.21\"","filePath":"F:\\test\\logs\\apache\\access_log","type":"XSS","status":"404"}]},{"attackCounts":9758,"telecomOperator":"","finishTime":"2020-12-03 16:05:46","isMalIp":false,"attackTypeAmount":9758,"ip":"192.168.0.114","latitude":"","location":"","startTime":"2020-12-02 13:55:59","attackTypeTotal":{"scanrobots":330,"FileInclude":4020,"Javaouinstream":6,"Sensitivefile":4785,"SQLInjection":6,"Webshell":12,"XSS":599},"longitude":"","attack":[]}]});

    查了官方文档,find 和 slice好像实现不了,需要用aggregate() 并使用两个单独的$project可是官方没有对应的例子,咋都凑不出来。

 

  • 写回答

1条回答 默认 最新

  • zhwITbird 2021-05-24 14:47
    关注

    找到合适的方式了,自己回答一下吧,用到的操作:

    aggregate:聚合,支持很多操作,目前一知半解

    $project :投影,我理解是对查询结果的数据结构进行重构

    $slice:对数组结构进行分页

    思路,通过两次$project()对查询结果进行两次投影:第一次:先去掉不需要的字段 attack ,第二次使用$slice对结果进行分页;

    第一步:先去掉不需要的字段,查询语句如下:

    aggregate([{$project:{"malIps.attack":0}}]).pretty();

    第二步:对malIps进行分页:

    aggregate([{$project:{"malIps":{$slice:["$malIps",0,1]}}}]).pretty();

    第三步:将两次的投影结合起来:

    aggregate([{$project:{"malIps":{$slice:["$malIps",0,1]}}},{$project:{"malIps.attack":0}}]).pretty();

    最终查询结果如下:

    {
            "_id" : ObjectId("60ab1cd22119d970838a7db8"),
            "malIps" : [
                    {
                            "attackCounts" : 9758,
                            "telecomOperator" : "",
                            "finishTime" : "2020-12-03 16:05:46",
                            "isMalIp" : false,
                            "attackTypeAmount" : 9758,
                            "ip" : "192.168.0.112",
                            "latitude" : "",
                            "location" : "",
                            "startTime" : "2020-12-02 13:55:59",
                            "attackTypeTotal" : {
                                    "scanrobots" : 330,
                                    "FileInclude" : 4020,
                                    "Javaouinstream" : 6,
                                    "Sensitivefile" : 4785,
                                    "SQLInjection" : 6,
                                    "Webshell" : 12,
                                    "XSS" : 599
                            },
                            "longitude" : ""
                    },
                    {
                            "attackCounts" : 9758,
                            "telecomOperator" : "",
                            "finishTime" : "2020-12-03 16:05:46",
                            "isMalIp" : false,
                            "attackTypeAmount" : 9758,
                            "ip" : "192.168.0.113",
                            "latitude" : "",
                            "location" : "",
                            "startTime" : "2020-12-02 13:55:59",
                            "attackTypeTotal" : {
                                    "scanrobots" : 330,
                                    "FileInclude" : 4020,
                                    "Javaouinstream" : 6,
                                    "Sensitivefile" : 4785,
                                    "SQLInjection" : 6,
                                    "Webshell" : 12,
                                    "XSS" : 599
                            },
                            "longitude" : ""
                    }
            ]
    }

    希望能帮到有同样疑惑的同学。。。

     

    参考:

    https://docs.mongodb.com/manual/reference/operator/projection/slice/

    https://docs.mongodb.com/manual/reference/method/db.collection.aggregate/

    https://docs.mongodb.com/manual/reference/operator/aggregation/project/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程