$log - Amazon DocumentDB

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

$log

4.0 版的新增内容。

Amazon DocumentDB 中的$log运算符计算数字的自然对数。它返回指定数字的以 e 为底的对数。

参数

  • expression:要计算自然对数的数字。

  • base:计算对数的基值。

示例(MongoDB 外壳)

以下示例演示如何使用运$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()