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

后端 未结 3 1047
旧时难觅i
旧时难觅i 2021-01-01 15:30

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

相关标签:
3条回答
  • 2021-01-01 16:15

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

    const kidDetails = await this.studentModel.find({ name:{$regex: new RegExp('.*' + studName, 'i') }} );
    
    0 讨论(0)
  • 2021-01-01 16:24

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

    db.scores.aggregate([
      {
        $match: {
          $expr: {
            $or: [
              { $gte: ["$score", 30] },
              { $lte: ["$score", 60] }
            ]
          }
        }
      },
      {
        $group: {
          _id: "$gamer",
          games: { $sum: 1 }
        }
      }
    ]);
    
    0 讨论(0)
  • 2021-01-01 16:29

    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.

    0 讨论(0)
提交回复
热议问题