mongodb

Using $push for null array


I have a mongo document which has array field called "events".

This field can sometimes be null.

I'm using $push to add an element to an "events" array. The problem is, it can't work when "events" field is null

For example, if in database it looks like this:

{
"_id" : ObjectId("4d2d8deff4e6c1d71fc29a07"),
"user_id" : "714638ba-2e08-2168-2b99-00002f3d43c0",
"events" : null
 }

and I run Update with $push:

{ $push : { "events" : { "profile" : 10, "data" : "X"}}};

it will fail because events is null.

I can fix this using 2 steps of querying the database. But, can I do it in 1 query?

I want to add the event to the array if the array fields is not null, but if it is null, first, create the "events" empty array, and later, add the event to the array.


Solution

  • Staring with MogoDB 4.2 you can use Updates with Aggregation Pipeline. This allows to perform the update as a single operation.

    var OBJ_TO_ADD = { "profile": 10, "data": "X" }
    
    db.test.updateOne(
      { "_id" : ObjectId("4d2d8deff4e6c1d71fc29a07") }, 
      [ 
          { 
              $set: { 
                  events: { 
                      $ifNull: [ 
                          { $concatArrays: [ "$events", [ OBJ_TO_ADD ] ] }, 
                          [ OBJ_TO_ADD ] 
                      ] 
                  }
              }
          } 
      ] 
    )