Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String to Int on AWS DocumentDB

I am currently trying to write a metabase question off AWS Document DB and I am running into an issue where I need to convert a string to an integer. Unfortunately, it seems like aws documentdb does not support $toInt and I am not sure how to get around it. Here is the query:

[
    {"$match": { 
        "metaData.fileSize" : {"$exists": true}
    }},
    {"$project": { 
        "file_size" : "$metaData.fileSize",
        "timestamp": 1,
        "past7Days": 
        { "$subtract": 
                [ ISODate(), 604800000]
        }
    }},
    {"$project": { 
        "file_size" : 1,
        "timestamp": 1,
        "dayofweek": {"$dayOfWeek":["$timestamp"]},
        "past7DaysComp": 
        { "$subtract": 
                [ "$timestamp", "$past7Days"]
        }
    }},
    {"$group" : 
            {   
                "_id" : {"dayofweek" : "$dayofweek"},
                "size": {"$avg" : "$file_size"}
            }
    }
]

The group returns nothing for size since it is not of a numeric type. Any ideas how to convert file_size to integer or double or float?

like image 952
Kevin Peng Avatar asked Aug 16 '26 05:08

Kevin Peng


1 Answers

As of 2023-11-11, the $convert and below similar conversion aggregation APIs have become available on AWS documentDB on v4.0+, according to the official doc from AWS

  • $convert
  • $toBool
  • $toDate
  • $toDecimal
  • $toDouble
  • $toInt
  • $toLong
  • $toObjectId
  • $toString
like image 105
ray Avatar answered Aug 19 '26 17:08

ray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!