本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
$toLower
Amazon DocumentDB 中的$toLower运算符用于将字符串转换为小写。
参数
示例(MongoDB 外壳)
以下示例演示如何使用$toLower运算符将Desk字段转换为小写。
创建示例文档
db.locations.insertMany([
{ "_id": 1, "Desk": "Düsseldorf-BVV-021" },
{ "_id": 2, "Desk": "Munich-HGG-32a" }
]);
查询示例
db.locations.aggregate([
{ $project: { item: { $toLower: "$Desk" } } }
]);
输出
{ "_id" : 1, "item" : "düsseldorf-bvv-021" }
{ "_id" : 2, "item" : "munich-hgg-32a" }
代码示例
要查看使用该$toLower命令的代码示例,请选择要使用的语言的选项卡:
- Node.js
-
const { MongoClient } = require("mongodb");
async function main() {
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("locations");
const result = await collection.aggregate([
{ $project: { item: { $toLower: "$Desk" } } }
]).toArray();
console.log(result);
await client.close();
}
main();
- Python
-
from pymongo import MongoClient
def main():
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["locations"]
result = list(collection.aggregate([
{ "$project": { "item": { "$toLower": "$Desk" } } }
]))
print(result)
client.close()
if __name__ == "__main__":
main()