Chapter 3: Creating and Manipulating Documents - Quiz: Wrong text

Hi MongoDB Developer Community!, I think this text is wrong since from my understanding that the query will not add an array (check the text in blue):

Please let me know if I’m wrong :sweat_smile:

Thanks in advance!

I can’t modify the text, so here is the direct link:

You are wrong.

See $push, pay attention to the fallowing from the Behaviour section:

If the field is absent in the document to update, $push adds the array field with the value as its element.

Edited to add example:

> c.find()
< { _id: 0 }
> c.updateOne( { _id : 0 } , { $push : { my_array : "steevej"}})
< { acknowledged: true,
    insertedId: null,
    matchedCount: 1,
    modifiedCount: 1,
    upsertedCount: 0 }
> c.find()
< { _id: 0, my_array: [ 'steevej' ] }
1 Like

Yes! you are right, thank you a lot!

Does anyone know how can I to delete this post? Finally it was not an error at all :see_no_evil:

1 Like

Please keep the post public. Others might have the same misunderstanding. The forum will be better if they can find an existing answer. It will also prevent frequent readers to read again and again the same issue.

See M001: Quiz: Updating Documents in the shell - #2 by steevej

1 Like

Great! Thanks again! :clap:t2:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.