3

In this json , I need a find query which finds all the field where the "status":"Y", if the parent field has "status":"N", ignore the child field , else find the child field where the "status":"Y" along with its parent field

Note: The sub field is in a array

[
  {
    "type": "Type 1",
    "status": "Y",
    "code": "1",
    "category": [
      {
        "type": "Cat 1",
        "status": "Y",
        "code": "1000",
        "subcategories": [
          {
            "type": "Sub 1",
            "status": "N",
            "code": "1001"
          },
          {
            "type": "Sub 2",
            "status": "N",
            "code": "1002"
          },
          {
            "type": "Sub 3",
            "status": "Y",
            "code": "1003"
          }
        ]
      },
      {
        "type": "Cat 2",
        "status": "N",
        "code": "2000",
        "subcategories": [
          {
            "type": "Sub 4",
            "status": "Y",
            "code": "2001"
          },
          {
            "type": "Sub 5",
            "status": "Y",
            "code": "2002"
          }
         ]
      }
    ]
  }
]

My Output Should be like this

[
  {
    "type": "Type 1",
    "status": "Y",
    "code": "1",
    "category": [
      {
        "type": "Cat 1",
        "status": "Y",
        "code": "1000",
        "subcategories": [
          {
            "type": "Sub 3",
            "status": "Y",
            "code": "1003"
          }
        ]
      }        ]
  }
]

Thanks in Advance:)

1 Answer 1

2

You can try below aggregation

db.collection.aggregate([
  { "$match": { "status": "Y" }},
  { "$unwind": "$category" },
  { "$match": { "category.status": "Y" } },
  { "$project": { "type": 1, "status": 1, "code": 1,
    "category.type": "$category.type",
    "category.status": "$category.status",
    "category.code": "$category.code",
    "category.subcategories": {
      "$filter": {
        "input": "$category.subcategories",
        "as": "subcategory",
        "cond": {
          "$eq": [
            "$$subcategory.status",
            "Y"
          ]
        }
      }
    }
  }},
  { "$group": {
    "_id": "$_id",
    "type": { "$first": "$type" },
    "status": { "$first": "$status" },
    "code": { "$first": "$code" },
    "category": { "$push": "$category" }
  }}
]).then((data) => {
  res.send(data)
})

Gives you following output (check here)

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "category": [
      {
        "code": "1000",
        "status": "Y",
        "subcategories": [
          {
            "code": "1003",
            "status": "Y",
            "type": "Sub 3"
          }
        ],
        "type": "Cat 1"
      }
    ],
    "code": "1",
    "status": "Y",
    "type": "Type 1"
  }
]
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.