Javascript Mongodb - 错误查询:BadValue 未知顶级运算符:$gte

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26755391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:20:00  来源:igfitidea点击:

Mongodb - bad query: BadValue unknown top level operator: $gte

javascriptmongodbmongodb-queryaggregation-framework

提问by Evaldas Raisutis

What is wrong with this query? I tried to run it on mongodb server and received an error as following - "exception: bad query: BadValue unknown top level operator: $gte". Can anyone tell me what is wrong with it, please?

这个查询有什么问题?我尝试在 mongodb 服务器上运行它并收到如下错误 - “异常:错误查询:BadValue 未知顶级运算符:$gte”。谁能告诉我它有什么问题吗?

        db.scores.aggregate([ 
            { 
                $match: { 
                    $or: [ 
                        { $gte: [ "$score", 30 ] }, 
                        { $lte: [ "$score", 60 ] } 
                    ] 
                } 
            },
            { 
                $group: { 
                    _id: "$gamer",
                    games: { $sum: 1 }
                } 
            }
        ])

sample data :

样本数据 :

        {
            "_id" : "545665cef9c60c133d2bce72",
            "score" : 85,
            "gamer" : "Latern"
        }

        /* 1 */
        {
            "_id" : "545665cef9c60c133d2bce73",
            "score" : 10,
            "gamer" : "BADA55"
        }

        /* 2 */
        {
            "_id" : "545665cef9c60c133d2bce74",
            "score" : 62,
            "gamer" : "BADA55"
        }

        /* 3 */
        {
            "_id" : "545665cef9c60c133d2bce75",
            "score" : 78,
            "gamer" : "l00ser"
        }

        /* 4 */
        {
            "_id" : "545665cef9c60c133d2bce76",
            "score" : 4,
            "gamer" : "l00ser"
        }

        /* 5 */
        {
            "_id" : "545665cef9c60c133d2bce77",
            "score" : 55,
            "gamer" : "FunnyCat"
        }

回答by Neil Lunn

You did this wrong. Should be:

你做错了。应该:

db.scores.aggregate([
    { "$match": {
        "score": { "$gte": 30, "$lte": 60 }
    }},
    { "$group": {
        "_id": "$gamer",
        "games": { "$sum": 1 }
    }}
])

Which is the proper way to specify a "range" query where the actual conditions are "and" and therefore "between" the operands specified.

这是指定“范围”查询的正确方法,其中实际条件是“和”,因此是“介于”指定的操作数。

回答by socrates

Since MongoDB version 3.6, you may use $exprto use aggregate expressions within a query. So the query can be rewritten into:

从 MongoDB 3.6 版开始,您可以使用$expr在查询中使用聚合表达式。所以查询可以改写为:

db.scores.aggregate([
  {
    $match: {
      $expr: {
        $or: [
          { $gte: ["$score", 30] },
          { $lte: ["$score", 60] }
        ]
      }
    }
  },
  {
    $group: {
      _id: "$gamer",
      games: { $sum: 1 }
    }
  }
]);

回答by kaviya bala

You need to include explicitly package in cmd prompt like npm i regexand change in service like as below:

您需要在 cmd 提示符中明确包含包, npm i regex并在服务中进行如下更改:

const kidDetails = await this.studentModel.find({ name:{$regex: new RegExp('.*' + studName, 'i') }} );