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年前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ