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

I need to put a filer on survey_id and user_id along with that I need to select all object inside QUESTIONLIST where isDelete is 1. I literally tried soo many thinngs but seems nothing is working or Am i putting it wrong ?? here is what all I tried

 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} ]})

1
  • Can you be more specific? What is the expected output? From the above example you want the first object of the questionList to be returned? Commented Sep 23, 2020 at 14:14

1 Answer 1

1

If your goal is to only get the relevant array elements from the questionList-array for documents that match the given survey_id and user_id, you can use the following aggregation to achieve this:

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 ]
          }
        }
      }
    }
  }
])

Check out the example that I've created on mongoplayground.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.