

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

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

`$cmp`Operator di Amazon DocumentDB digunakan untuk membandingkan dua nilai dan mengembalikan nilai integer yang menunjukkan urutan relatifnya. Ini adalah operator perbandingan yang membandingkan dua ekspresi dan mengembalikan nilai integer -1, 0, atau 1, tergantung pada apakah nilai pertama kurang dari, sama dengan, atau lebih besar dari nilai kedua, masing-masing.

**Parameter**
+ `expression1`: Ekspresi pertama untuk membandingkan.
+ `expression2`: Ekspresi kedua untuk membandingkan.

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

Contoh berikut menunjukkan penggunaan `$cmp` operator untuk membandingkan dua nilai numerik.

**Buat dokumen sampel**

```
db.collection.insertMany([
  { _id: 1, value1: 10, value2: 20 },
  { _id: 2, value1: 15, value2: 15 },
  { _id: 3, value1: 20, value2: 10 }
]);
```

**Contoh kueri**

```
db.collection.find({
  $expr: {
    $cmp: ["$value1", "$value2"]
  }
})
```

**Keluaran**

```
[
  { "_id" : 1, "value1" : 10, "value2" : 20 },
  { "_id" : 3, "value1" : 20, "value2" : 10 }
]
```

Dalam contoh ini, `$cmp` operator membandingkan `value2` bidang `value1` dan untuk setiap dokumen. Hasilnya adalah:

```
- `$cmp: ["$value1", "$value2"]` returns -1 for the first document (10 < 20), 0 for the second document (15 = 15), and 1 for the third document (20 > 10).
```

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

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

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

Berikut adalah contoh penggunaan `$cmp` operator dalam aplikasi Node.js dengan `mongodb` driver:

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

const client = new MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false');

async function main() {
  await client.connect();
  const db = client.db('test');
  const collection = db.collection('mycollection');

  // Insert sample documents
  await collection.insertMany([
    { _id: 1, value1: 10, value2: 20 },
    { _id: 2, value1: 15, value2: 15 },
    { _id: 3, value1: 20, value2: 10 }
  ]);

  // Query using $cmp operator
  const result = await collection.find({
    $expr: {
      $cmp: ['$value1', '$value2']
    }
  }).toArray();

  console.log(result);

  await client.close();
}

main();
```

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

Berikut adalah contoh penggunaan `$cmp` operator dalam aplikasi Python dengan driver: `pymongo`

```
from pymongo import MongoClient

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['mycollection']

# Insert sample documents
collection.insert_many([
    {'_id': 1, 'value1': 10, 'value2': 20},
    {'_id': 2, 'value1': 15, 'value2': 15},
    {'_id': 3, 'value1': 20, 'value2': 10}
])

# Query using $cmp operator
result = list(collection.find({
    '$expr': {
        '$cmp': ['$value1', '$value2']
    }
}))

print(result)

client.close()
```

Output dari contoh Node.js dan Python akan sama dengan contoh MongoDB Shell:

```
[
  { "_id" : 1, "value1" : 10, "value2" : 20 },
  { "_id" : 2, "value1" : 15, "value2" : 15 },
  { "_id" : 3, "value1" : 20, "value2" : 10 }
]
```

------