기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
$rtrim
버전 4.0의 새 버전입니다.
Elastic 클러스터에서는 지원되지 않습니다.
Amazon DocumentDB의 $rtrim 연산자는 문자열에서 후행 문자를 제거하는 데 사용됩니다. 기본적으로 후행 공백 문자를 제거하지만 문자 인수를 전달하여 제거할 문자 집합을 지정할 수도 있습니다.
파라미터
예제(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()