Phaedrus Design
Phaedrus Design

Reputation: 31

how do you trim the result of mongodb's $arrayElemAt when it has extra white space?

this is code i have:

$addFields: {
    category: {
        // $product_categories => ['  test ', 'foo']
        $arrayElemAt: ['$product_categories', 0] 
    },
}

the 'category' field will be assign to ' test ', from the database, from which i want to trim the extra space, front and back, the result should be 'test' without the extra space, is this possible? how to do it?? thanks

Upvotes: 1

Views: 103

Answers (1)

Dheemanth Bhat
Dheemanth Bhat

Reputation: 4452

If you are using MongoDB version > 4.0, you can use $trim operator. So try this:

$addFields: {
  category: {
    // $product_categories => ['  test ', 'foo']
    $trim: {
      input: { $arrayElemAt: ['$product_categories', 0] }
    }
  }
}

Upvotes: 1

Related Questions