

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# \$1arrayToObject
<a name="arrayToObject"></a>

`$arrayToObject`Operator di Amazon DocumentDB adalah kebalikan dari operator. `$objectToArray` Dibutuhkan array dokumen pasangan kunci-nilai dan mengubahnya menjadi satu dokumen. Ini sangat berguna ketika Anda perlu mengubah array pasangan kunci-nilai kembali ke objek atau struktur dokumen.

**Parameter**
+ `array expression`: Ekspresi yang menyelesaikan ke array. Elemen array harus dokumen dengan dua bidang: `k` (kunci) dan `v` (nilai).

## Contoh (MongoDB Shell)
<a name="arrayToObject-examples"></a>

Contoh di bawah ini menunjukkan bagaimana menggunakan `$arrayToObject` untuk mengkonversi array pasangan kunci-nilai kembali ke dokumen.

**Buat dokumen sampel**

```
db.videos.insertMany([
  { "_id": 1, "name": "Live Soft", "inventory": { "Des Moines": 1000, "Ames": 500 } },
  { "_id": 2, "name": "Top Pilot", "inventory": { "Mason City": 250, "Des Moines": 1000 } },
  { "_id": 3, "name": "Romancing the Rock", "inventory": { "Mason City": 250, "Ames": 500 } },
  { "_id": 4, "name": "Bravemind", "inventory": { "Mason City": 250, "Des Moines": 1000, "Ames": 500 } }
]);
```

**Contoh kueri**

```
db.videos.aggregate([
  { $project: { name: 1, videos: { $objectToArray: "$inventory" } } },
  { $project: { name: 1, inventory: { $arrayToObject: "$videos" } } }
]);
```

**Keluaran**

```
{ "_id" : 1, "name" : "Live Soft", "inventory" : { "Des Moines" : 1000, "Ames" : 500 } }
{ "_id" : 2, "name" : "Top Pilot", "inventory" : { "Mason City" : 250, "Des Moines" : 1000 } }
{ "_id" : 3, "name" : "Romancing the Rock", "inventory" : { "Mason City" : 250, "Ames" : 500 } }
{ "_id" : 4, "name" : "Bravemind", "inventory" : { "Mason City" : 250, "Des Moines" : 1000, "Ames" : 500 } }
```

Dalam contoh ini, `$objectToArray` operator digunakan untuk mengubah `inventory` objek menjadi array pasangan kunci-nilai. `$arrayToObject`Operator kemudian digunakan untuk mengubah array kembali menjadi dokumen, memulihkan struktur objek asli.

## Contoh kode
<a name="arrayToObject-code"></a>

Untuk melihat contoh kode untuk menggunakan `$arrayToObject` perintah, pilih tab untuk bahasa yang ingin Anda gunakan:

------
#### [ Node.js ]

```
const { MongoClient } = require('mongodb');

async function example() {
  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('videos');

  const result = await collection.aggregate([
    { $project: { name: 1, videos: { $objectToArray: '$inventory' } } },
    { $project: { name: 1, inventory: { $arrayToObject: '$videos' } } }
  ]).toArray();

  console.log(result);
  await client.close();
}

example();
```

------
#### [ Python ]

```
from pymongo import MongoClient

def example():
    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['videos']

    result = list(collection.aggregate([
        { '$project': { 'name': 1, 'videos': { '$objectToArray': '$inventory' } } },
        { '$project': { 'name': 1, 'inventory': { '$arrayToObject': '$videos' } } }
    ]))

    print(result)
    client.close()

example()
```

------