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년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠