$log10 - Amazon DocumentDB

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

$log10

Baru dari versi 4.0.

$log10Operator di Amazon DocumentDB digunakan untuk menghitung logaritma basis-10 dari suatu angka. Hal ini berguna untuk melakukan perhitungan logaritmik pada bidang numerik dalam pipeline agregasi.

Parameter

  • expression: Ekspresi numerik yang logaritma basis-10 akan dihitung.

Contoh (MongoDB Shell)

Contoh berikut menunjukkan bagaimana menggunakan $log10 operator untuk menghitung logaritma basis-10 dari bidang numerik.

Buat dokumen sampel

db.numbers.insertMany([ { _id: 1, value: 1 }, { _id: 2, value: 10 }, { _id: 3, value: 100 }, { _id: 4, value: 1000 } ]);

Contoh kueri

db.numbers.aggregate([ { $project: { _id: 1, log10Value: { $log10: "$value" } } } ]);

Keluaran

[ { "_id": 1, "log10Value": 0 }, { "_id": 2, "log10Value": 1 }, { "_id": 3, "log10Value": 2 }, { "_id": 4, "log10Value": 3 } ]

Contoh kode

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

Node.js
const { MongoClient } = require('mongodb'); async function example() { let client; try { 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: 1, log10Value: { $log10: "$value" } } } ]).toArray(); console.log(result); } catch (error) { console.error("An error occurred:", error); } finally { if (client) { await client.close(); } } } example();
Python
from pymongo import MongoClient def example(): client = None try: 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': 1, 'log10Value': { '$log10': '$value' } } } ])) print(result) except Exception as e: print(f"An error occurred: {e}") finally: if client: client.close() example()