'How to convert from string to number in nodejs mongoose

How can I convert publicationDate from string to number/int using mongoose on nodejs? I would like to then sort my data by year. Thank you.

Here is my code :

app.get("/getAlbumData.json",(req,res)=> {
    Wasabi.find( {publicationDate: { $in: ['2009','2010','2011','2012','2013','2014','2015','2016'] } }, function (err, doc) {
            res.send(doc);
            res.end();

    }).limit(200)
})


Solution 1:[1]

You can convert it into a Decimal, and then use .toNumber, here is an example if you already have moongose imported:

import Decimal from "decimal.js";

const yourNumber = new Decimal(yourString).toNumber();

the const yourNumber is now in type number, not Number.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lucas Tulha