【问题标题】:How do I solve: 'MongoError: $where is not allowed in this atlas tier'?如何解决:'MongoError: $where is not allowed in this atlas tier'?
【发布时间】:2019-06-09 13:12:18
【问题描述】:

如何解决:使用 MongoDB Atlas 时出现“MongoError: $where is not allowed in this atlas tier”?

这是我的代码:

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 
};

当我使用我的本地 mongoDB 实例时,它可以工作,但是一旦我开始使用 mongoAtlas,我就会收到错误:$where is not allowed in this atlas tier。我该如何解决?

【问题讨论】:

    标签: mongodb mongodb-query


    【解决方案1】:

    我刚刚被告知免费层 (M0)、M2 和 M5 实例有一些限制和命令限制,包括有限的分析指标。因此,我必须升级以获得更多功能....

    【讨论】:

    【解决方案2】:

    虽然您使用的 Atlas 层可能不支持 $where,但好消息是您不需要在此查询中使用它。

    改为使用如下查询:

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

    一般来说,无论如何,您都只想将$where 用作最后的手段,因为它效率不高。

    【讨论】:

    • 如何使用 mongoose 按月获取数据,我使用 where 条件但 git 给出了 $where 不允许在此 atlas 层我的代码就像 var month = req.params.createdAt; Data.find({"$where": this.createdAt.toJSON() .slice(6, 7) == ${month} ,}) 怎么解决?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    • 2022-09-27
    • 2016-06-20
    相关资源
    最近更新 更多