AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。

$concatArrays(式演算子)

$concatArrays

配列を連結して、連結された配列を返します。

$concatArrays の構文は次のとおりです。

{ $concatArrays: [ <array1>, <array2>, ... ] }

<array>式は、配列に変換される限り、有効な式であれば何でも使用できます。 式の詳細については、「式 」を参照してください。

いずれかの引数がnullの値に解決されるか、欠落しているフィールドを参照する場合、 $concatArraysはnullを返します。

例
結果
{ $concatArrays: [ [ "hello", " "], [ "world" ]] }
[ "hello", " ", "world" ]
{ $concatArrays: [ [ "hello", " "], [ [ "world" ], "again"]] }
[ "hello", " ", [ "world" ], "again" ]

次のドキュメントを含むwarehousesという名前のコレクションを作成します。

db.warehouses.insertMany( [ { _id : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] }, { _id : 2, instock: [ "apples", "pudding", "pie" ] }, { _id : 3, instock: [ "pears", "pecans" ], ordered: [ "cherries" ] }, { _id : 4, instock: [ "ice cream" ], ordered: [ ] }] )

次の例では、instock 配列と ordered 配列を連結します。

db.warehouses.aggregate( [ { $project: { items: { $concatArrays: [ "$instock", "$ordered" ] } } }] )
{ _id : 1, items : [ "chocolate", "butter", "apples" ] }{ _id : 2, items : null }{ _id : 3, items : [ "pears", "pecans", "cherries" ] }{ _id : 4, items : [ "ice cream" ] }
このページを評価

項目一覧