翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
$trim
バージョン 4.0 の新機能
Amazon DocumentDB の $trim演算子は、文字列から先頭および/または末尾の空白文字を削除するために使用されます。
パラメータ
例 (MongoDB シェル)
次の例は、 $trim演算子を使用して文字列から先頭と末尾の空白を削除する方法を示しています。
サンプルドキュメントを作成する
db.people.insertMany([
{ "name": " John Doe " },
{ "name": " Bob Johnson " }
])
クエリの例
db.people.aggregate([
{ $project: {
"name": { $trim: {input: "$name"}}
}}
])
出力
[
{ "name": "John Doe" },
{ "name": "Bob Johnson" }
]
コードの例
$trim コマンドを使用するコード例を表示するには、使用する言語のタブを選択します。
- 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('people');
const result = await collection.aggregate([
{ $project: {
"name": { $trim: {input: "$name" }}
}}
]).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['people']
result = list(collection.aggregate([
{"$project": {
"name": {"$trim": {"input": "$name"}}
}}
]))
print(result)
client.close()
example()