如何在mongodb中找到嵌入式嵌套数组的长度? [英] How to find length of embedded nested array in mongodb?

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

问题描述

如何在mongodb中查找嵌入式嵌套数组的长度?例如, 我的mongoDB中有以下集合. loc字段包含坐标字段,该字段是一个数组,再次包含另一个索引为0的数组. 如何找到loc.corrdinates [0]的长度? 我试图通过使用聚合管道来做到这一点,但是它给出了语法错误:

How to find length of embedded nested array in mongodb? For example, I have the below collection in my mongoDB. The loc field contains coordinates field which is an array and again it contains another array which is in 0th index. How can I find length of loc.corrdinates[0] ? I tried to do this with using aggregation pipeline but it gives syntax error:

db.atolldata.aggregate([
                        {$project:{_id:0,
                                   fileName:1,
                         sizeOfArray:{$size:"$loc.coordinates[0]"}}}]); 

.

这是我的收藏集

db.mydb.findOne();

db.mydb.findOne();

{
    "_id" : ObjectId("54ccb36fe4b0ff85abfee006"),
    "_class" : "com.inn.signaleye.model.AtollData",
    "rsrp" : "-124",
    "index" : 1,
    "rgb" : [
        192,
        157,
        0
    ],
    "loc" : {
        "type" : "Polygon",
        "coordinates" : [
            [
                [
                    91.5254213240325,
                    26.225096
                ],
                [
                    91.5254099392226,
                    26.225999
                ],
                [
                    91.52591034644685,
                    26.226004
                ],
                [
                    91.52592172739666,
                    26.225101
                ],
                [
                    91.5254213240325,
                    26.225096
                ]
            ]
        ]
    },
    "fileName" : "RSRP_ZOne46N_20m_SCFT.shp"
}

推荐答案

使用

Make use of the $unwind stage, to flatten the coordinates array, and then find the size in the $project stage.

db.atolldata.aggregate([
{$unwind:"$loc.coordinates"},
{$project:{"_id":0,"fileName":1,
           "sizeOfArray":{$size:"$loc.coordinates"}}}
])

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

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