$ltrim - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$ltrim

New from version 4.0.

Not supported by Elastic cluster.

The $ltrim operator in Amazon DocumentDB is used to remove leading characters from a string. By default, it removes leading whitespace characters, but you can also specify a set of characters to remove by passing the chars argument.

Parameters

  • input: The input string from which to remove leading whitespace characters.

  • chars: (optional) To remove specific characters.

Example (MongoDB Shell)

The following example demonstrates the usage of $ltrim to remove specified characters (" *") from the beginning of a string.

Create sample documents

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

Query example

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

Output

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

Code examples

To view a code example for using the $ltrim command, choose the tab for the language that you want to use:

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()