Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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
Mongodb mongoose-根据条件从查询中排除嵌入文档_Mongodb_Mongoose_Aggregation Framework - Fatal编程技术网

Mongodb mongoose-根据条件从查询中排除嵌入文档

Mongodb mongoose-根据条件从查询中排除嵌入文档,mongodb,mongoose,aggregation-framework,Mongodb,Mongoose,Aggregation Framework,我在用猫鼬。我的数据库中有以下文档: question: { "id": "60ab9bd0d40a362189e842ff", "deletedAt": null "answers": [ { "id": "60ab9e4e58a72f4768c5f63b", "dele

我在用猫鼬。我的数据库中有以下文档:

question: {
    "id": "60ab9bd0d40a362189e842ff",
    "deletedAt": null
        "answers": [
        {
            "id": "60ab9e4e58a72f4768c5f63b",
            "deletedAt": "2021-05-28T20:23:30.409Z",
        },
        {
            "id": "60ab9e4e58a72f4768c5f64c",
            "deletedAt": null,
        },
    }
}
我想得到
deletedAt
字段为空的所有问题,以及
deletedAt
字段为空的所有答案。 因此,我的查询结果应该是:

question: {
    "id": "60ab9bd0d40a362189e842ff",
    "deletedAt": null
        "answers": [
         {
            "id": "60ab9e4e58a72f4768c5f64c",
            "deletedAt": null,
        },
    }
}
我所尝试的:

Question.aggregate([
    { $match: { deletedAt: null }},
    { $project: {
        answers: { 
          $filter: {
            input: "$answers",
            as: "answer",
            cond: { "$$answer.deletedAt": null }
          }
        }
      }
    },
    { $sort: sort },
    { $limit: limit + 1 }
]}

我将非常感谢任何形式的帮助

使用
$eq
运算符匹配
$filter
运算符中的条件

    { $project: {
        answers: { 
          $filter: {
            input: "$answers",
            as: "answer",
            cond: { 
              $eq: ["$$answer.deletedAt", null]
            }
          }
        }
      }
    },