AI 에이전트의 경우: 문서 인덱스는 https://www.mongodb.com/ko-kr/docs/llms.txt에서 사용할 수 있으며, 모든 페이지의 마크다운 버전은 어떤 URL 경로에 .md를 추가하여 사용할 수 있습니다.
Docs Menu

$zip (표현식 연산자)

$zip

출력 배열의 첫 번째 요소가 첫 번째 입력 배열의 첫 번째 요소, 두 번째 입력 배열의 첫 번째 요소 등을 포함하는 배열이 되도록 입력 배열의 배열을 바꿉니다.

For example, $zip would transform [ [ 1, 2, 3 ], [ "a", "b", "c" ] ] into [ [ 1, "a" ], [ 2, "b" ], [ 3, "c" ] ].

$zip 의 구문은 다음과 같습니다:

{
$zip: {
inputs: [ <array expression1>, ... ],
useLongestLength: <boolean>,
defaults: <array expression>
}
}
피연산자
설명

inputs

배열로 해석되는 표현식의 배열입니다. 이러한 입력 배열의 요소는 결합되어 출력 배열의 배열을 형성합니다.

If any of the inputs arrays resolves to a value of null or refers to a missing field, $zip returns null.

If any of the inputs arrays does not resolve to an array or null nor refers to a missing field, $zip returns an error.

useLongestLength

가장 긴 배열의 길이가 출력 배열의 배열 수를 결정하는지 여부를 지정하는 부울입니다.

기본값은 false이며 가장 짧은 배열 길이에 따라 출력 배열의 배열 수가 결정됩니다.

defaults

An array of default element values to use if the input arrays have different lengths. You must specify useLongestLength: true along with this field, or else $zip will return an error.

If useLongestLength: true but defaults is empty or not specified, $zip uses null as the default value.

If specifying a non-empty defaults, you must specify a default for each input array or else $zip will return an error.

The input arrays do not need to be of the same length. By default, the output array has the length of the shortest input array, but the useLongestLength option instructs $zip to output an array as long as the longest input array.

예시
결과
{ $zip: { inputs: [ [ "a" ], [ "b" ], [ "c" ] ] }
[ [ "a", "b", "c" ] ]
{ $zip: { inputs: [ [ "a" ], [ "b", "c" ] ] } }
[ [ "a", "b" ] ]
{
$zip: {
inputs: [ [ 1 ], [ 2, 3 ] ],
useLongestLength: true
}
}
[ [ 1, 2 ], [ null, 3 ] ]
{
$zip: {
inputs: [ [ 1 ], [ 2, 3 ], [ 4 ] ],
useLongestLength: true,
defaults: [ "a", "b", "c" ]
}
}

useLongestLength: true이므로 $zip은 더 짧은 입력 배열을 해당하는 defaults 요소로 채웁니다.

결과는 [ [ 1, 2, 4 ], [ "a", 3, "c" ] ]입니다.

컬렉션 matrices에는 다음 문서가 포함되어 있습니다.

db.matrices.insertMany([
{ matrix: [[1, 2], [2, 3], [3, 4]] },
{ matrix: [[8, 7], [7, 6], [5, 4]] },
])

이 컬렉션에 있는 각 3x2 매트릭스 전치를 계산하려면 다음 집계 작업을 사용하면 됩니다.

db.matrices.aggregate([{
$project: {
_id: false,
transposed: {
$zip: {
inputs: [
{ $arrayElemAt: [ "$matrix", 0 ] },
{ $arrayElemAt: [ "$matrix", 1 ] },
{ $arrayElemAt: [ "$matrix", 2 ] },
]
}
}
}
}])

그러면 다음과 같은 2x3 매트릭스가 반환됩니다.

{ "transposed" : [ [ 1, 2, 3 ], [ 2, 3, 4 ] ] }
{ "transposed" : [ [ 8, 7, 5 ], [ 7, 6, 4 ] ] }

$zip을 $filter와 함께 사용하여 배열에서 요소의 하위 집합을 가져와서 유지되는 각 요소와 함께 원래 인덱스를 저장할 수 있습니다.

pages 컬렉션에는 다음 문서가 포함되어 있습니다.

db.pages.insertOne( {
"category": "unix",
"pages": [
{ "title": "awk for beginners", reviews: 5 },
{ "title": "sed for newbies", reviews: 0 },
{ "title": "grep made simple", reviews: 2 },
] } )

다음 집계 파이프라인은 먼저 pages 배열의 요소를 인덱스와 함께 압축한 다음 리뷰가 하나 이상 있는 페이지만 필터링합니다.

db.pages.aggregate([{
$project: {
_id: false,
pages: {
$filter: {
input: {
$zip: {
inputs: [ "$pages", { $range: [0, { $size: "$pages" }] } ]
}
},
as: "pageWithIndex",
cond: {
$let: {
vars: {
page: { $arrayElemAt: [ "$$pageWithIndex", 0 ] }
},
in: { $gte: [ "$$page.reviews", 1 ] }
}
}
}
}
}
}])

그러면 다음 문서가 반환됩니다.

{
"pages" : [
[ { "title" : "awk for beginners", "reviews" : 5 }, 0 ],
[ { "title" : "grep made simple", "reviews" : 2 }, 2 ] ]
}
이 페이지 평가하기

이 페이지의 내용