Hướng dẫn mongodb size of nested array - kích thước mongodb của mảng lồng nhau

Làm thế nào để tìm chiều dài của mảng lồng được nhúng trong MongoDB? Ví dụ, tôi có bộ sưu tập dưới đây trong MongoDB của tôi. Trường LOC chứa trường tọa độ là một mảng và một lần nữa nó chứa một mảng khác nằm trong chỉ số 0. Làm thế nào tôi có thể tìm thấy độ dài của loc.corrdinates [0]? Tôi đã cố gắng thực hiện điều này với việc sử dụng đường ống tổng hợp nhưng nó gây ra lỗi cú pháp:

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

.

Đây là bộ sưu tập của tôi

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"
}

Tài liệu về nhà → Hướng dẫn sử dụng MongoDBMongoDB Manual

$sizeCounts và trả về tổng số mục trong một mảng.

Counts and returns the total number of items in an array.

$size có cú pháp sau: has the following syntax:

Đối số cho $size có thể là bất kỳ biểu thức nào miễn là nó giải quyết thành một mảng. Để biết thêm thông tin về biểu thức, xem biểu thức.$size can be any expression as long as it resolves to an array. For more information on expressions, see Expressions.

Đối số cho $size phải giải quyết cho một mảng. Nếu đối số cho $size bị thiếu hoặc không giải quyết được một mảng, $size lỗi.$size must resolve to an array. If the argument for $size is missing or does not resolve to an array, $size errors.

Xem xét một bộ sưu tập

{
    "_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"
}
1 với các tài liệu sau:

{ "_id" : 1, "item" : "ABC1", "description" : "product 1", colors: [ "blue", "black", "red" ] }
{ "_id" : 2, "item" : "ABC2", "description" : "product 2", colors: [ "purple" ] }
{ "_id" : 3, "item" : "XYZ1", "description" : "product 3", colors: [ ] }
{ "_id" : 4, "item" : "ZZZ1", "description" : "product 4 - missing colors" }
{ "_id" : 5, "item" : "ZZZ2", "description" : "product 5 - colors is string", colors: "blue,red" }

Hoạt động đường ống tổng hợp sau đây sử dụng toán tử $size để trả về số lượng phần tử trong mảng

{
    "_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"
}
3:$size operator to return the number of elements in the
{
    "_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"
}
3 array:

db.inventory.aggregate([
{
$project: {
item: 1,
numberOfColors: { $cond: { if: { $isArray: "$colors" }, then: { $size: "$colors" }, else: "NA"} }
}
}
] )

Hoạt động trả về như sau:

{ "_id" : 1, "item" : "ABC1", "numberOfColors" : 3 }
{ "_id" : 2, "item" : "ABC2", "numberOfColors" : 1 }
{ "_id" : 3, "item" : "XYZ1", "numberOfColors" : 0 }
{ "_id" : 4, "item" : "ZZZ1", "numberOfColors" : "NA" }
{ "_id" : 5, "item" : "ZZZ2", "numberOfColors" : "NA" }

Làm thế nào để tôi tìm thấy độ dài của một mảng trong MongoDB?

Toán tử kích thước $ phù hợp với bất kỳ mảng nào với số lượng các phần tử được chỉ định bởi đối số.Ví dụ: DB.thu thập. matches any array with the number of elements specified by the argument. For example: db. collection.

Làm thế nào để tôi tìm thấy kích thước của một bộ sưu tập trong MongoDB?

Phương thức tổng số () được sử dụng để báo cáo tổng kích thước của một bộ sưu tập, bao gồm kích thước của tất cả các tài liệu và tất cả các chỉ mục trên một bộ sưu tập.Trả về: Tổng kích thước tính bằng byte của dữ liệu trong bộ sưu tập cộng với kích thước của mọi chỉ mục trên bộ sưu tập. is used to reports the total size of a collection, including the size of all documents and all indexes on a collection. Returns: The total size in bytes of the data in the collection plus the size of every index on the collection.

Kích thước $ trong MongoDB là gì?

$ Kích thước.Đếm và trả về tổng số lượng mục trong một mảng.$ Size có cú pháp sau: {$ size:} Đối số cho kích thước $ có thể là bất kỳ biểu thức nào miễn là nó giải quyết thành một mảng.Counts and returns the total the number of items in an array. $size has the following syntax: { $size: } The argument for $size can be any expression as long as it resolves to an array.

Dự án $ trong MongoDB là gì?

Dự án $ lấy một tài liệu có thể chỉ định bao gồm các trường, việc triệt tiêu trường _ID, bổ sung các trường mới và đặt lại các giá trị của các trường hiện có.Ngoài ra, bạn có thể chỉ định loại trừ các trường.Chỉ định bao gồm một trường.takes a document that can specify the inclusion of fields, the suppression of the _id field, the addition of new fields, and the resetting of the values of existing fields. Alternatively, you may specify the exclusion of fields. Specifies the inclusion of a field.