Home > OS >  MongoDB script creates an empty object when it finds a null object instead of ignoring it, how can I
MongoDB script creates an empty object when it finds a null object instead of ignoring it, how can I

Time:10-02

I have a large script, the following section is the relevant part, but if people prefer I can post the whole script on request.
The script iterates through a list, looking for a field called colour and renames it color.

{
    serviceAgreementRefList: {
      $map : {
        input: "$$this.relatedJson.serviceAgreementRefList",
        in: {

           $mergeObjects: [
              "$$this",
               {
                  $cond: [
                     {                                                                              
                       $ne: [                                                                                          
                          "$$this.situation",                                                                                         
                          undefined
                          ]
                     },
                                                                                                                                                                           
                     "situation": {
                                                                                            
                        $mergeObjects: [
                           "$$this.situation",
                           {
                             color: "$$this.situation.colour",
                           }
                        ]
                     }
                 },
                {},
              ]
             }
            ]
           }
          }

        }
   }

It works as expected for the most part, however, if the object situation exists but is null then the script creates an empty situation object.
I would like any null situation objects to remain null.

How can I achieve that?

I thought I could add an $or function to the $cond, but of course that doesn't worse and in fact makes the problem worse.
Can I use an $in function, containing two $ne functions, one for undefined and one for null?
I would imagine I can, but I can't get the syntax right.

CodePudding user response:

So the solution here is to use the $eq function instead of $ne and compare the type of the field to what we're expecting it to be.
If the type matches what we expect then the field name will be modified, if it doesn't, then nothing will be changed.
this is what the $cond function should look like:

          $cond: [
             {                                                                              
               $eq: [                                                                                          
                  { $type: "$$this.situation" },                                                                                         
                  "object"
                  ]
             },
             { <code to execute if true> },
             { <code to execute if false> }
          ]

if situation is populated then $type will return "object", if it is null then $type will return "null" and if it does not exist $type will return "missing".

  • Related