JSON syntax error in "MongoDB Documents" lecture

An invalid JSON document structure was presented in the lecture video:

...
"Jobs": [
    "TotalJobs": 1,
    {
        "Company": "MongoForms",
        "YearsWorked": 2
    }
]
...

Help me out here, I’m missing what’s wrong…

This looks like a proper MongoDB field to me. Mind you, this is not the whole doc, just one field.

Maybe the question is

Can an array have a combination of different data types?

edit: to fix my shocking grammar

Why yes, it looks like they can :slight_smile: An array could even contain more documents :smiley:

https://docs.mongodb.com/manual/tutorial/query-array-of-documents/

Dang! MongoDB really is a bit too much like Inception, right?

1 Like

Arrays can contain objects (aka documents), but arrays in JSON can’t contain key-value pairs – only objects can. In the example the syntax would become valid only if we enclose “TotalJobs”: 1 in curly braces, which doesn’t provide an intended document structure anyway:

...
"Jobs": [
    { "TotalJobs": 1 },
    {
        "Company": "MongoForms",
        "YearsWorked": 2
    }
],
...
2 Likes

Thanks for such an awesome catch!

We will be adding this to lecture notes. :slight_smile:

Kanika