0
{
            "_id": "5f6b45ad73cac785f0e7504c",
            "user_id": "zzzz",
            "survey_id": "40",
            "questionList": [
                {
                    "isDelete": 1,
                    "choices": [
                        {
                            "_id": "5f6b45ad73cac785f0e75068",
                            "name": "Choice1",
                            "value": "",
                            "score": 12,
                            "disqualifier": "true"
                        },
                        {
                            "_id": "5f6b45ad73cac785f0e75067",
                            "name": "choice2",
                            "value": "",
                            "score": 21,
                            "disqualifier": "true"
                        },
                        {
                            "_id": "5f6b45ad73cac785f0e75066",
                            "name": "choice3",
                            "value": "",
                            "score": 21,
                            "disqualifier": "false"
                        }
                    ],
                    "_id": "5f6b45ad73cac785f0e75065",
                    "type": "mcq",
                    "name": "Multi Choice",
                    "value": "choice2",
                    "isMandatory": "true"
                },
                {
                    "isDelete": 0,
                    "choices": [
                        {
                            "_id": "5f6b45ad73cac785f0e75064",
                            "name": "option1",
                            "value": "",
                            "score": 12,
                            "disqualifier": "true"
                        },
                        {
                            "_id": "5f6b45ad73cac785f0e75063",
                            "name": "option2",
                            "value": "",
                            "score": 12,
                            "disqualifier": "false"
                        },
                        {
                            "_id": "5f6b45ad73cac785f0e75062",
                            "name": "option3",
                            "value": "",
                            "score": 33,
                            "disqualifier": "false"
                        }
                    ],
                    "_id": "5f6b45ad73cac785f0e75061",
                    "type": "dropdown",
                    "name": "dropdown",
                    "value": "option2",
                    "isMandatory": "true"
                }

我需要在survey_id 和user_id 上放置一个文件管理器,同时我需要选择QUESTIONLIST 中isDelete 为1 的所有对象。我确实尝试了很多东西,但似乎没有任何效果,或者我错了吗?这就是我所尝试的

 await surveyfinal.find({$and:[{survey_id:req.body.survey_id},{user_id:req.body.user_id},{questionList:{$elemMatch:{isDelete:1}}}]})

await surveyfinal.find({user_id:req.body.user_id},{questionList:{$elemMatch:{isDelete:1}}} )

await surveyfinal.find({$and:[{user_id:req.body.user_id},{survey_id:req.body.survey_id},{"questionList.isDelete":1} ]})

4

1 回答 1

0

如果您的目标是仅从questionList-array 中获取与给定survey_idand匹配的文档的相关数组元素,则user_id可以使用以下聚合来实现此目的:

db.collection.aggregate([
  {
    $match: {
      "user_id": "zzzz", "survey_id": "40", "questionList.isDelete": 1
    }
  },
  {
    $project: {
      questionList: {
        $filter: {
          input: "$questionList",
          as: "questionList",
          cond: {
            $eq: [ "$$questionList.isDelete", 1 ]
          }
        }
      }
    }
  }
])

查看我在mongoplayground创建的示例。

于 2020-09-23T14:25:48.360 回答