$rtrim - Amazon DocumentDB

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

$rtrim

Baru dari versi 4.0.

Tidak didukung oleh cluster elastis.

$rtrimOperator di Amazon DocumentDB digunakan untuk menghapus karakter trailing dari string. Secara default, ini menghapus karakter spasi tambahan, tetapi Anda juga dapat menentukan satu set karakter yang akan dihapus dengan meneruskan argumen karakter.

Parameter

  • input: String masukan dari mana untuk menghapus karakter spasi belakang.

  • chars: (opsional) Untuk menghapus karakter tertentu.

Contoh (MongoDB Shell)

Contoh berikut menunjukkan bagaimana menggunakan $rtrim operator untuk menghapus trailing karakter tertentu (“*”) dari string.

Buat dokumen sampel

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

Contoh kueri

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

Keluaran

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

Contoh kode

Untuk melihat contoh kode untuk menggunakan $rtrim perintah, pilih tab untuk bahasa yang ingin Anda gunakan:

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