data type converstion in DocumentDB using aggregation pipelines

1

I have a MongoDB aggregation pipeline that converts a string to a double. I just realized that DocumentDB doesn't support the $convert or $toDouble operators. Is there any workaround for this problem while still using an aggregation pipeline?

Below is an extract from my pipeline that works in MongoDB Atlas:

...
{
    $convert:{
        input:$subDoc.stringVar,
        to:'double',
        'onError':0,
        'onNull':0
    }
}, 
...
已提問 2 年前檢視次數 1464 次
1 個回答
0

Hello. You could use the Javascript parseFloat() method and programmatically update those strings to double in the existing collection. Or use an $out stage in the aggregation and update the results afterwards. Something like this will loop through all documents in the collection and update the string:

db.collection.find({}).forEach( function(doc) {
  doc.subDoc.stringVar = parseFloat( doc.subDoc.stringVar );
  db.collection.update( { _id: doc._id }, {$set: { "subDoc.stringVar": doc.subDoc.stringVar } } )
 });
AWS
Mihai A
已回答 2 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南