Reputation: 31
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
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