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
    }
}, 
...
demandé il y a 2 ans1464 vues
1 réponse
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
répondu il y a 2 ans

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions