View a markdown version of this page

$sortArray - Amazon DocumentDB

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

$sortArray

バージョン 8.0 の新機能

Elastic クラスターではサポートされていません。

Amazon DocumentDB の $sortArray演算子は、配列の要素をソートします。スカラー値の配列の場合、配列は要素値でソートされます。ドキュメントの配列の場合、配列は指定されたフィールドでソートされます。演算子は、ソートされた配列を昇順 (1) または降順 (-1) で返します。

注記

$sortArray は、クエリプランナーバージョン 3 の集約パイプラインの$projectステージでサポートされています。

[Syntax] (構文)

{ $sortArray: { input: <array>, sortBy: <sort specification> } }

パラメータ

  • input: ソートする配列。

  • sortBy: ソート順。スカラー値の配列の場合は、昇順1に 、降順-1に を指定します。ドキュメントの配列には、フォームのドキュメントを指定する{ field: 1 }か、ソートする{ field: -1 }各フィールドを指定します。

例 (MongoDB シェル)

次の例は、 $sortArray演算子を使用して配列の要素を昇順でソートする方法を示しています。

サンプルドキュメントを作成する

db.miles.insertMany([ { "_id" : 1, "member_since" : ISODate("1987-01-01T00:00:00Z"), "credit_card" : false, "flight_miles" : [ 1205, 2560, 880 ]}, { "_id" : 2, "member_since" : ISODate("1982-01-01T00:00:00Z"), "credit_card" : true, "flight_miles" : [ 1205, 2560, 890, 2780]}, { "_id" : 3, "member_since" : ISODate("1999-01-01T00:00:00Z"), "credit_card" : true, "flight_miles" : [ 1205, 880]} ]);

クエリの例

db.miles.aggregate([ { $project: { _id: 1, member_since: 1, credit_card: 1, sorted_flight_miles: { $sortArray: { input: "$flight_miles", sortBy: 1 } } } } ]);

出力

{ "_id" : 1, "member_since" : ISODate("1987-01-01T00:00:00Z"), "credit_card" : false, "sorted_flight_miles" : [ 880, 1205, 2560 ] } { "_id" : 2, "member_since" : ISODate("1982-01-01T00:00:00Z"), "credit_card" : true, "sorted_flight_miles" : [ 890, 1205, 2560, 2780 ] } { "_id" : 3, "member_since" : ISODate("1999-01-01T00:00:00Z"), "credit_card" : true, "sorted_flight_miles" : [ 880, 1205 ] }

この例では、$sortArray演算子はflight_miles配列を昇順でソートします。出力の結果sorted_flight_milesフィールドには、配列の要素がソート順に表示されます。

コードの例

$sortArray コマンドを使用するコード例を表示するには、使用する言語のタブを選択します。

Node.js

Node.js アプリケーションで $sortArray演算子を使用する例を次に示します。

const { MongoClient } = require('mongodb'); async function sortArray() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('miles'); const result = await collection.aggregate([ { $project: { _id: 1, member_since: 1, credit_card: 1, sorted_flight_miles: { $sortArray: { input: '$flight_miles', sortBy: 1 } } } } ]).toArray(); console.log(result); client.close(); } sortArray();
Python

Python アプリケーションで $sortArray演算子を使用する例を次に示します。

from pymongo import MongoClient def sort_array(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client.test collection = db.miles result = list(collection.aggregate([ { '$project': { '_id': 1, 'member_since': 1, 'credit_card': 1, 'sorted_flight_miles': { '$sortArray': { 'input': '$flight_miles', 'sortBy': 1 } } } } ])) print(result) client.close() sort_array()