First class objects

In chapter 1, lesson “Basic Reads”, there is a TEST:

The lecturer said the phrase:
“Because ‘cast’ is an array, MongoDB will look at all elements of the array to match this. This is because MongoDB treats arrays as first class objects”.
What does it mean? What are first class objects?

Hi @Victor_69915,

Great question!

This means that arrays elements will be used to match single value matches.
Expressing a query over an array field value, does not require you to decompose or express the query in such a way that you need format it based on the field type, when that field is an array.

As an example:

{ "_id": 1, "a": "hello" }
{ "_id": 2, "a": ["hello", 123] }
{ "_id": 3, "a": 123 }

For this collection, you might be interested in finding all documents that match a: "hello". Given that in the document with _id: 2 field a is represented by an array, MongoDB matches that value against all the array elements.

db.collection.find({"a": "hello" })
{ "_id": 1, "a": "hello" }
{ "_id": 2, "a": ["hello", 123] }

But it also matches if we are actually looking for all documents where the field a is in fact an array of two elements

db.collection.find({"a":{"$size": 2}})
{ "_id": 2, "a": ["hello", 123] }

Or if a particular element of the array field is of a given value

{ "_id": 2, "a": ["hello", 123] }

So we keep treating all array fields has they should, with their particular set of operators, but we do not discard allow matches over the elements of the array, treating this type as first class objects.

This is what we mean by first class objects.
Does that make sense ?



Thanks! This is very useful information.:+1: