"$push turns field into an array field if it was of different type"

Hi,

in Chapter 4 - Adanced CRUD the Video in Lecture: Array Operators states at ~00:30 the following:

$push - Turns a field into an array field if it previously was of a different type.

Doesn’t this confliect with reference documentation: https://docs.mongodb.com/manual/reference/operator/update/push/

Especially:

Behaviour

If the field is not an array, the operation will fail.

Am i missing something?

thx

Just bad wording.

db.collection.insert( { goodbye: "carlos" } )

db.collection.update({},
{
  "$push": {
    a: "hello"
  }
})

Returns:

[
  {
    "a": [
      "hello"
    ],
    "goodbye": "carlos"
  }
]

So a was converted to an array.

Now push to goodbye:

db.collection.update({},
{
  "$push": {
    goodbye: "hello"
  }
})

It returns:

write errors: field ‘goodbye’ must be an array but is of type string in document

Feel free to test here.

Hi there,

alright - to me this reads as if an already existing (non-array) property/field on the document would be converted to an array using $push…
trying that, i obv got the very same error you mention - got confused by that.

thx for clarifying.

kind regards, dd

Hi @Daniel_Dietz,

Thanks for surfacing this. We will look into it.