Using MongoDB 4.0 I have to compute a new field basing on existing field. An example of documents in mycollection:
{
_id: ...,
f1: 'a',
f2: 'b'
}
My python code, after connecting to DB:
Please note that
myJoin
is here just for example. In real use scenario this function is quite complex (custom cryptographic calculations and bytes manipulations).
def myJoin(left, right):
return left+'-myDelimiter-'+right
myPipeline = [
{
'$match': {
'f1': {
'$exists': True
}
}
}, {
'$addFields': {
'f3': myJoin('$f1', '$f2')
}
}
]
mydb.mycollection.aggregate(myPipeline)
However in DB I see:
{
_id: ...,
f1: 'a',
f2: 'b',
f3: '$f1-myDelimiter-$f2'
}
But I want:
{
_id: ...,
f1: 'a',
f2: 'b',
f3: 'a-myDelimiter-b'
}
For the moment I am using pipeline aggregations. But other strategies are welcome.