如何找到数组mongodb的长度 [英] How to find length of the array mongodb

查看:46
本文介绍了如何找到数组mongodb的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库有这种类型的结构.我想找到我的JAN"数组包含的文档数量.我正在尝试以不同的方式做,但没有任何效果.如果有人帮忙那就太好了.

I have such this type of structure of my database. I want to find number of documents my "JAN" array contains. I am trying to do in different way but nothing works. That would be great if someone helps.

 {
            "_id" : ObjectId("5cd8609db20663a19e2c362c"),
            "2017" : {
                "JAN" : [
                    {
                        "SYMBOL" : "20MICRONS",
                        "SERIES" : "EQ",
                        "OPEN" : "33.4",
                        "HIGH" : "34.3",
                        "LOW" : "33",
                    },
                    {
                        "SYMBOL" : "3IINFOTECH",
                        "SERIES" : "EQ",
                        "OPEN" : "5.8",
                        "HIGH" : "5.8",
                        "LOW" : "5.55",
                    },
                    {
                        "SYMBOL" : "3MINDIA",
                        "SERIES" : "EQ",
                        "OPEN" : "11199.9",
                        "HIGH" : "11233",
                        "LOW" : "10861",
                    }
                ]
            }
        }

推荐答案

您可以使用 $size 运算符求数组的长度

You can use $size operator to find the length of the array

db.collection.aggregate([
  {
    "$project": {
      "totalJan": {
        "$size": "$2017.JAN"
      }
    }
  }
])

MongoPlayground

这篇关于如何找到数组mongodb的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆