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