Finding certain data based on certain conditions

Hi there,
I am working on a nodejs project and have setup mongodb. I need help with finding the data from mongodb based on certain conditions. The condition is that, in “stations” collection, there is “stationContent” array having objects in it. Each object has a type property. I want to read this type and fetch the values based on it. For example, if the type = “Explanation Module”, I want to remove the “options” property from this specific object. I want to do the same conditional checks with all of the objects “stationContent” array.
Your help would greatly be appreciated.

please provide sample input documents and sample results. to come up with a solution we need to experiment with real documents that we can cut-n-paste into our system

This post was flagged by the community and is temporarily hidden.

Please read Formatting code and log snippets in posts and update your documents so that we can use them.

your quotes are still wrong

Hi, sorry for the wrong syntax. Now I have verified and corrected the json. Please check it.
As required, here is the sample document of “stations” collection that I am referring to in the question:

{

  "_id": {

    "$oid": "6322d7f4acdcb979e04bbffa"

  },

  "content": {

    "$oid": "632035cff65b9178d8386e64"

  },

  "stationContent": [

    {

      "_id": {

        "$oid": "6322d7f4acdcb979e04bbffb"

      },

      "type": "Explanation Module",

      "question": "This is a test question",

      "options": [],

      "pairs": [],

      "single_strings": [],

      "single_strings_two": []

    },

    {

      "_id": {

        "$oid": "6322d7f4acdcb979e04bbffc"

      },

      "type": "Multiple choice",

      "question": "This is 2nd test question",

      "options": [

        {

          "_id": {

            "$oid": "6322d7f4acdcb979e04bbffd"

          },

          "trueFalse": true,

          "description": "This is description of first option",

          "createdAt": {

            "$date": {

              "$numberLong": "1663227892923"

            }

          }

        },

        {

          "_id": {

            "$oid": "6322d7f4acdcb979e04bbffe"

          },

          "trueFalse": false,

          "description": "This is description of second option"

        }

      ],

      "comment": {

        "comment_by": true,

        "comment_text": "this is comment text"

      },

      "pairs": [],

      "single_strings": [],

      "single_strings_two": []

    },

    {

      "_id": {

        "$oid": "6322d7f4acdcb979e04bbfff"

      },

      "type": "Fill in the blank",

      "question": "question for fill in blank",

      "true_false": false,

      "comment": {

        "comment_by": true,

        "comment_text": "test text for fill blank"

      },

      "options": [],

      "pairs": [],

      "single_strings": [],

      "single_strings_two": []

    },

    {

      "_id": {

        "$oid": "6322d7f4acdcb979e04bc000"

      },

      "type": "True/False question",

      "question": "quesitno for true/false",

      "question_type_true": true,

      "true_false": false,

      "options": [],

      "pairs": [],

      "single_strings": [],

      "single_strings_two": []

    },

    {

      "_id": {

        "$oid": "6322d7f4acdcb979e04bc001"

      },

      "type": "Choose the blanks",

      "question": "question for choose blanks",

      "true_false": true,

      "string_one": "sdlfjsdlkfsdlk",

      "options": [],

      "pairs": [],

      "single_strings": [],

      "single_strings_two": []

    },

    {

      "_id": {

        "$oid": "6322d7f4acdcb979e04bc009"

      },

      "type": "Pairs",

      "question": "test question for pairs type",

      "pairs": [

        {

          "_id": {

            "$oid": "6322d7f4acdcb979e04bc00a"

          },

          "string_one": "sdlkfjsdlkf",

          "string_two": "sdklfsdfkjlk",

          "createdAt": {

            "$date": {

              "$numberLong": "1663227892923"

            }

          },

          "updatedAt": {

            "$date": {

              "$numberLong": "1663227892923"

            }

          }

        },

        {

          "_id": {

            "$oid": "6322d7f4acdcb979e04bc00b"

          },

          "string_one": "dfsdfsfsd",

          "string_two": "fgdfgdfgdfgf",

          "createdAt": {

            "$date": {

              "$numberLong": "1663227892923"

            }

          },

          "updatedAt": {

            "$date": {

              "$numberLong": "1663227892923"

            }

          }

        }

      ],

      "options": [],

      "single_strings": [],

      "single_strings_two": [],

      "createdAt": {

        "$date": {

          "$numberLong": "1663227892923"

        }

      },

      "updatedAt": {

        "$date": {

          "$numberLong": "1663227892923"

        }

      }

    }

  ],

  "__v": 0

}

Basically, I want to query for all the documents of the “stations” collection. And in each document of stations collection, there is “stationContent” array; in that array, there are objects, each object has a type property, and this is what I want to base my conditions on.
So for example, if the object has type as “Explanation Module”, I want to deselect the options, pairs, single_strings, and single_strings_two properties. Because these are empty. I don’t want to send the empty data to the frontend.
Similarly, if the object has type “Multiple choice”, I want to deselect the pairs, single_strings, and single_strings_two properties.
Hope I explained it as expected.
Thank you so much

What you need is an $addFields stage. This stage will use $map on the stationContent array. The expression will use a $cond on the type field to map $$this to a new object that exclude the fields you want to get remove.

Personally, I leave this type of data cosmetic to the front end or application layer. Especially, when it is only to remove empty data.