

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

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

Baru dari versi 4.0

`$toInt`Operator di Amazon DocumentDB digunakan untuk mengonversi nilai input ke tipe data integer. Operator ini berguna ketika Anda perlu memastikan bahwa bidang atau ekspresi direpresentasikan sebagai bilangan bulat, yang dapat menjadi penting untuk operasi tertentu atau tugas pemrosesan data.

**Parameter**
+ `expression`: Ekspresi yang akan dikonversi ke integer.

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

Contoh berikut menunjukkan bagaimana menggunakan `$toInt` operator untuk mengkonversi nilai string ke integer.

**Buat dokumen sampel**

```
db.numbers.insertMany([
  { "name": "one", "value": "1" },
  { "name": "hundred", "value": "100" }
]);
```

**Contoh kueri**

```
db.numbers.aggregate([
  { $project: {
    "_id": 0,
    "name": 1,
    "intValue": { $toInt: "$value" }
  }}
]);
```

**Keluaran**

```
{ "name": "one", "intValue": 1 }
{ "name": "hundred", "intValue": 100 }
```

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

Untuk melihat contoh kode untuk menggunakan `$toInt` 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('numbers');

  const result = await collection.aggregate([
    { $project: {
      "_id": 0,
      "name": 1,
      "intValue": { $toInt: "$value" }
    }}
  ]).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['numbers']

    result = list(collection.aggregate([
        { "$project": {
            "_id": 0,
            "name": 1,
            "intValue": { "$toInt": "$value" }
        }}
    ]))

    print(result)
    client.close()

example()
```

------