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