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 MongoDB → MongoDB Manual
$size
Counts 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" }