Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Mongodb-错误查询:BadValue未知顶级运算符:$gte_Javascript_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

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

Javascript Mongodb-错误查询:BadValue未知顶级运算符:$gte,javascript,mongodb,mongodb-query,aggregation-framework,Javascript,Mongodb,Mongodb Query,Aggregation Framework,这个查询有什么问题?我试图在mongodb服务器上运行它,收到如下错误-“异常:错误查询:BadValue未知顶级运算符:$gte”。谁能告诉我它有什么毛病吗 db.scores.aggregate([ { $match: { $or: [ { $gte: [ "$score", 30 ] },

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

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

        {
            "_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"
        }

你做错了。应该是:

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

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

自MongoDB 3.6版以来,您可以在查询中使用聚合表达式。因此,可以将查询重写为:

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

您需要在cmd提示符中显式地包含包,如
npm i regex
和服务变更如下:

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

这可能会帮助像我这样的人。。。在我的例子中,
字段
为空。并遇到此错误未知顶级运算符:$in,MongoDb异常

private <T> void addQueryCondition (Query query, String field , List<T> valuesList ) {
    if (!CollectionUtils.isEmpty(valuesList)) {
        query.addCriteria(Criteria.where(field).in(valuesList).exists(true));
    }
}
private void addQueryCondition(查询、字符串字段、列表值列表){
如果(!CollectionUtils.isEmpty(valuesList)){
query.addCriteria(Criteria.where(field).in(valuesList.exists)(true));
}
}
关键点在于此,因此您希望使用而不是。(顺便说一句,通过使用