$rtrim operator inside $map

Hi,
can anyone explain why rtrim operator is not working in the following context:

db.movies.aggregate([
  {$match:{
    "writers":{$elemMatch:{$exists:true}}
    }
  },{$project:{
    "_id":0,
    "title":1,
    "writers":{
        $map:{
             input:"$writers",
             as:"writer",
             in: {$rtrim:{
                     input:{$arrayElemAt:[{$split:["$$writer","("]},0]},
                     chars:" " 
                    }
                } 
             }
      }      
  	}
  }
]);

I receive the following error when I execute it:

2019-09-23T10:03:13.737+0300 E QUERY [js] Error: command failed: {
ā€œoperationTimeā€ : Timestamp(1569222188, 1),
ā€œokā€ : 0,
ā€œerrmsgā€ : ā€œUnrecognized expression ā€˜$rtrimā€™ā€,
ā€œcodeā€ : 168,
ā€œcodeNameā€ : ā€œInvalidPipelineOperatorā€,
ā€œ$clusterTimeā€ : {
ā€œclusterTimeā€ : Timestamp(1569222188, 1),
ā€œsignatureā€ : {
ā€œhashā€ : BinData(0,ā€œZ4MavG7sAV58pz3Ca/mm2lr+U4Q=ā€),
ā€œkeyIdā€ : NumberLong(ā€œ6698341139830800385ā€)
}
}
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:583:17
assert.commandWorked@src/mongo/shell/assert.js:673:16
DB.prototype._runAggregate@src/mongo/shell/db.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1012:12

Thanks,
Florin

Hey @Florin_96146

Perhaps check which version of mongo you are using with

mongo --version

Hi, thank you for the reply.
My client version is:

PS C:\Users\FDavid> mongo --version
MongoDB shell version v4.2.0

The query is executed against Atlas database provided as support for aggregation course.

Thanks,
FD

Hey @Florin_96146

So the Atlas Cluster running the database in version 3.6

$rtrim was introduced in v4.0

So your pipeline looks correct but would have to be run against a version 4.0 or higher mongod

Hope this helps.

1 Like

Yes, thank you. I didn’t pay attention to that detail.