0

Documents in my MongoDB collection look like this:

My Mongo version is 4.2.3

{
            "_id": "SAGE-UW-00005",
            "carriers": [
                {
                    "bindable": true,
                    "carrierCode": "LMICO",
                    "mapped": true,
                    "products": [
                        {
                            "industries": [
                                {
                                    "industryCode": null,
                                     "states": "GA"
                                }
                            ],
                            "isAllNCCIValid": null,
                            "isAllstateValid": true,
                            
                        }
                    ],
                    "questionCode": "LMGENRL17"
                }
            ],
            "column": 1,
            "dataType": null,
            
    }

This is my desired output:

{
        "_id": "SAGE-UW-00005",
        "carriers": [
            {
                "bindable": true,
                "carrierCode": "LMICO",
                "mapped": true,
                "products": [
                    {
                        "industries": [
                            {
                          
                                 "states": "GA"
                            }
                        ],
      
                        "isAllstateValid": true,
                        
                    }
                ],
                "questionCode": "LMGENRL17"
            }
        ],
        "column": 1,
     
        
    }

I am not sure the depth of nested subdocuments in the collection, but there should be a lot of null fields in the collection. My backend code uses $exists to query the fields in the collection, so null is creating a problem here.

  • Are you looking for [Remove all fields that are null](https://stackoverflow.com/questions/25287204/remove-all-fields-that-are-null) – Gibbs Aug 31 '20 at 08:05
  • @Gibbs, yes. The solution in the link doesn't seem to be working for me, –  Aug 31 '20 at 08:49
  • In general, you need to know the fields you are trying to update (or remove). That will make your task easier. There are features like [Aggregation Updates](https://docs.mongodb.com/v4.2/tutorial/update-documents-with-aggregation-pipeline/index.html) and [Array Update Operators](https://docs.mongodb.com/v4.2/reference/operator/update-array/index.html) you can take advantage of. – prasad_ Aug 31 '20 at 08:50
  • @prasad_ gotcha. Will look into it. –  Aug 31 '20 at 14:00

1 Answers1

0

I am not sure the depth of nested subdocuments in the collection, but there should be a lot of null fields in the collection

It is a dynamic question. Best option would be replace the document after removing null fields in the code.

As you have nested levels, I would suggest you to map your data to pojo and check whether any entry and field is null. Unless you aware of the fields, it is not efficient to remove them.

Gibbs
  • 21,904
  • 13
  • 74
  • 138