Reading Arrays from Documents
Explore working with arrays in MongoDB, including querying for specific values, using operators like $in, $nin, $all, and $size, to filter and manipulate array data in documents.
We'll cover the following...
Understanding arrays in MongoDB
In MongoDB, an array is a list of values within a field of a document. These values can be of any data type:
Strings:
["reading", "cycling", "music"]Numbers:
[1, 2, 3, 4]Objects:
[ { "name": "Alice" }, { "name": "Bob" } ]Nested arrays:
[ [1, 2], [3, 4] ]
Visually, we can see them as below.
A MongoDB document can have multiple values for a single field. In our case, the users collection contains a field, orderHistory, which is an array of objects. See the snippet below.
In the document above, the orderHistory field has an array, which contains four objects: "id": "3000", "id": "3001", "id": "3002", and "id": "3003". ...