翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
$rtrim
バージョン 4.0 の新機能。
Elastic クラスターではサポートされていません。
Amazon DocumentDB の $rtrim演算子は、文字列から末尾の文字を削除するために使用されます。デフォルトでは、末尾の空白文字は削除されますが、chars 引数を渡すことで削除する文字のセットを指定することもできます。
パラメータ
例 (MongoDB シェル)
次の例は、 $rtrim演算子を使用して文字列から末尾の指定された文字 (「 *」) を削除する方法を示しています。
サンプルドキュメントを作成する
db.collection.insert([
{ "name": "John Doe* ", "age": 30 },
{ "name": "Jane Smith ", "age": 25 },
{ "name": "Bob Johnson", "age": 35 }
]);
クエリの例
db.collection.aggregate([
{
$project: {
_id: 0,
name: { $rtrim: { input: "$name", chars: " *" } },
age: 1
}
}
]);
出力
[
{ age: 30, name: 'John Doe' },
{ age: 25, name: 'Jane Smith' },
{ age: 35, name: 'Bob Johnson' }
]
コードの例
$rtrim コマンドを使用するためのコード例を表示するには、使用する言語のタブを選択します。
- Node.js
-
const { MongoClient, Bson } = 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 {
const db = client.db('test');
const collection = db.collection('collection');
const pipeline = [
{
$project: {
_id: 0,
name: { $rtrim: { input: "$name", chars: " *" } },
age: 1
}
}
];
const results = await collection.aggregate(pipeline).toArray();
console.dir(results, { depth: null });
} catch (err) {
console.error('Error occurred:', err);
} finally {
await client.close();
}
}
example().catch(console.error);
- 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')
try:
db = client.test
collection = db.collection
pipeline = [
{
"$project": {
"_id": 0,
"name": { "$rtrim": { "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()