$log - Amazon DocumentDB

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

$log

4.0 版的新功能。

Amazon DocumentDB 中的$log運算子會計算數字的自然對數。它會傳回指定數字的 base-e 對數。

參數

  • expression:將計算自然對數的數字。

  • base:用於計算日誌的基本值。

範例 (MongoDB Shell)

下列範例示範運算$log子用來計算數字的自然對數。

建立範例文件

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

查詢範例

db.numbers.aggregate([ { $project: { _id: 1, naturalLog: { $log: ["$value", 10] } }} ]);

輸出

[ { "_id" : 1, "naturalLog" : 1 }, { "_id" : 2, "naturalLog" : 2 }, { "_id" : 3, "naturalLog" : 2.9999999999999996 } ]

程式碼範例

若要檢視使用 $log命令的程式碼範例,請選擇您要使用的語言標籤:

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: 1, naturalLog: { $log: ["$value", 10] } }} ]).toArray(); console.log(result); 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": 1, "naturalLog": { "$log": ["$value", 10] } }} ])) print(result) client.close() example()