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
    }
}, 
...
feita há 2 anos1464 visualizações
1 Resposta
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
respondido há 2 anos

Você não está conectado. Fazer login para postar uma resposta.

Uma boa resposta responde claramente à pergunta, dá feedback construtivo e incentiva o crescimento profissional de quem perguntou.

Diretrizes para responder a perguntas