$ltrim - Amazon DocumentDB

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

$ltrim

バージョン 4.0 の新機能。

Elastic クラスターではサポートされていません。

Amazon DocumentDB の $ltrim演算子は、文字列から先頭文字を削除するために使用されます。デフォルトでは、先頭の空白文字は削除されますが、chars 引数を渡すことで削除する文字のセットを指定することもできます。

パラメータ

  • input: 先頭の空白文字を削除する入力文字列。

  • chars: (オプション) 特定の文字を削除します。

例 (MongoDB シェル)

次の例は、 を使用して文字列の先頭から指定された文字 (「 *」) $ltrimを削除する方法を示しています。

サンプルドキュメントを作成する

db.collection.insertMany([ { name: " *John Doe", age: 30 }, { name: "Jane Doe*", age: 25 }, { name: " Bob Smith ", age: 35 } ]);

クエリの例

db.collection.aggregate([ { $project: { _id: 0, name: { $ltrim: { input: "$name", chars: " *" } }, age: 1 } } ]);

出力

[ { "name": "John Doe", "age": 30 }, { "name": "Jane Doe ", "age": 25 }, { "name": "Bob Smith ", "age": 35 } ]

コードの例

$ltrim コマンドを使用するコード例を表示するには、使用する言語のタブを選択します。

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'); try { await client.connect(); const db = client.db('test'); const collection = db.collection('collection'); const pipeline = [ { $project: { _id: 0, name: { $ltrim: { input: '$name', chars: ' *' } }, age: 1 } } ]; const result = await collection.aggregate(pipeline).toArray(); console.dir(result, { depth: null }); } finally { await client.close(); } } example().catch(console.error);
Python
from pymongo import MongoClient def example(): try: 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.collection pipeline = [ { "$project": { "_id": 0, "name": { "$ltrim": { "input": "$name", "chars": " *" } }, "age": 1 } } ] results = collection.aggregate(pipeline) for doc in results: print(doc) except Exception as e: print(f"An error occurred: {e}") finally: client.close() example()