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

你离开我真会死。 提交于 2020-06-28 07:46:45

问题


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?


回答1:


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.




回答2:


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....



来源:https://stackoverflow.com/questions/54200300/how-do-i-solve-mongoerror-where-is-not-allowed-in-this-atlas-tier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!