Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I solve: 'MongoError: $where is not allowed in this atlas tier'?

How do I solve: 'MongoError: $where is not allowed in this atlas tier' when using MongoDB Atlas?

Here is my code:

async function getEventsTakingPlace(){
    const getEventsTakingPlace = await event.find({$where: function() { 
        return ((this.eventTime < new Date()) && (new Date(this.eventTime).getTime()+ 100*60000 > new Date().getTime() ) ) }},
        function(err, events){
        if(err){
            return err;
        }else {
            return events; 
        }
    }); 
    return getEventsTakingPlace 
};

when I use my local mongoDB instance it works but once i start ising mongoAtlas I get the error: $where is not allowed in this atlas tier. How do I solve this?

like image 790
YulePale Avatar asked Jan 15 '19 13:01

YulePale


2 Answers

I have just been told that the free tier (M0), M2 and M5 Instances have some limitations and command limitations, including limited metrics for analysis. I therefore have to upgrade to get more features....

like image 76
YulePale Avatar answered Sep 22 '22 02:09

YulePale


While $where may not be supported by the Atlas tier you're using, the good news is that you don't need to use it for this query.

Instead, use a query like:

let now = new Date();
let min = new Date();
min.setTime(now.getTime() - 100*60000);
const events = await event.find({eventTime: {$gt: min, $lt: now}});

In general, you only want to use $where as a last resort anyway, as it's not efficient.

like image 31
JohnnyHK Avatar answered Sep 19 '22 02:09

JohnnyHK