Mongoose:如何通过聚合框架将 $gte 和 $lte 用于数组

Mongoose: How to use $gte and $lte into array with Aggregation framework

我有一个 mongoDB 对象,例如:

{
"Questions":[
  {
    "mark": 10
  },
  {
    "mark": 2
  },
  {
    "mark": 7
  }
 ]
}

我要return这样大于2小于8的标记

输出应如下所示:

{
"Questions":[ 
  {
    "mark": 2
  },
  {
    "mark": 7
  }
 ]
}

演示 - https://mongoplayground.net/p/jFhksSRIFA4

使用$filter to get filtered array using $gte and $lte

db.collection.aggregate([
  {
    $project: {
      Questions: {
        $filter: {
          "input": "$Questions",
          "as": "q",
          "cond": {
            $and: [
              { $gte: [ "$$q.mark", 2] },
              { $lte: [ "$$q.mark", 8] }
            ]
          }
        }
      }
    }
  }
])