- Introduction
- Find Specific Array Value No Matter Order or Size
- Find List of Array Values With the Same Order and Size
- $all : Find List of Array Values With the Same Size
- $in : Find Arrays Contains At Least One of the Values in the Provided Array
- $nin : Find Arrays that Does Not contain any of the Values in the Provided Array
- $elemMatch : Find Arrays Contains At Least One Element that Matches one or more Criteria
- $size : Find Arrays that have a Certain Size
- $slice : Limit Number of Elements returned from an Array Field
In the world of NoSQL databases, MongoDB has gained significant popularity for its flexibility and powerful querying capabilities. One of its standout features is the ability to store arrays within documents, allowing for efficient organization and retrieval of complex data structures.
In this article, I will take you on a journey to facilitate the process of querying arrays in MongoDB documents. Whether you're a beginner or an experienced MongoDB user, this guide will provide you with the essential knowledge and practical examples to effectively query arrays and unlock the true potential of your data.
Definition:
To find documents where the specified array field contains a specific value, no matter order or size of this array, we just need to pass this value as it without using any operators.
Syntax:
<arrayField>: <value>
Examples:
db.array.find({items:200});
This query returns all documents where the items
field is an array that contains the value 200
at least once. It does not consider the position of the value within the array or any other elements in the array.
If the items
field is an array that contains other values in addition to 200
, this query will still return the documents that contain 200
.
Definition:
The term Exact Match
refers to find documents where the specified array field contain list of values with the same order and size.
Syntax:
<arrayField>: [ <value1> , <value2> ... ]
Examples:
db.array.find({items:[100,200]});
This query will return all the documents where the items
field is an array that exactly matches the values [100, 200]
in that order.
Note that if a document contains the array [50, 100, 200, 300]
, it will not
be returned by the above query, because the values are not in the exact order [100, 200]
or size.
However, if you want to find documents that contain both 100 and 200 regardless of their order, you can use the $all
operator, as will be explained in the next section.
Definition:
the $all
operator is used to match documents where the specified field contains all the specified elements in the provided array.
The order of the elements in the array does not matter. Also, the $all
operator matches documents that contain additional elements in the array
Syntax:
<arrayField>: { $all: [ <value1> , <value2> ... ] }
Examples:
db.array.find({items:{$all:[100,200]}});
The query searches for documents in the array
collection where the items
field contains both the values 100 and 200, regardless of their order and maybe contain additional elements in the array.
We can get the same result using the $and
operator :
db.array.find({$and:[{items:100},{items:200}]});
Definition:
the $in
operator is used to match documents where the specified field contain either any of the values in the provided array.
The $in
operator takes an array of values as its argument and returns all documents where the specified field contain at leaset one of those values.
Syntax:
<arrayField>: { $in: [ <value1>, <value2> ... ] }
Examples:
db.array.find({items: {$in:[200,300]}});
This query searches for documents in the array
collection and returns only those documents where the items
field contain the values 200
or 300
.
We can achieve the same result using the $or
operator :
db.array.find({$or:[{items:200},{items:300}]});
Definition:
the $nin
operator is used to match documents where the specified field does not contain any of the values in the provided array.
The $nin
operator takes an array of values as its argument and returns all documents where the specified field does not contain any of those values.
Syntax:
<arrayField>: { $nin: [ <value1>, <value2> ... ] }
Examples:
db.array.find({items: {$nin:[200,300]}});
This query searches for documents in the array
collection and returns only those documents where the items
field does not contain
the values 200
or 300
.
The above query is equivalent to the following query :
db.array.find({ items: { $not: { $in: [200, 300] } } });
Definition:
The $elemMatch
operator find documents that contain an array field with at least one element that matches one or more criteria.
Syntax:
<arrayField>: { $elemMatch: { <criteria_1>, <criteria_2>, ... } }
Examples:
(1)
db.array.find({ items:{ $elemMatch: {$eq:300} } });
The previous query finds documents in the array
collection where the items
field has a value of 300
.
(2)
db.array.find({ items: { $elemMatch: { $gt: 100, $lt: 200} } });
Find documents where the items
field has value that are greater than 100
and less than 200
(3)
db.array.find({ items: { $elemMatch: { $nin: [100,200]} } });
Where the items
array field contains at least one element that is not equal to 100 or 200
Definition:
The $size
operator find documents that contain an array field with a specific number of elements - certain size - no matter the order of the elements.
The operator only checks the number of elements in the array and does not consider the order or content of those elements.
Syntax:
<arrayField>: { $size: <number> }
Examples:
db.array.find({items:{$size:3}})
The $size
operator in the example is used to match documents where the items
field has exactly three elements.
Definition:
The $slice
operator is used to limit the number of elements returned from an array field or retrieve a subset of elements from an array field.
It is consider a projection opertator as it comes as a second argument in the find
method
Syntax:
{ <arrayField>: { $slice: <number> } }
//or
{ <arrayField>: { $slice: [ <number_to_skip>, <number_to_return> ] } }
Examples:
(1)
db.array.find({}, {items:{$slice:1}});
The previous query finds all documents in the array
collection and returns only the first
element in the items
array field for each document.
(2) You can also use the $slice
operator with negative
values to return a certain number of elements from the end
of the array field:
db.array.find({items:{$size:3}}, {items:{$slice:-1}});
The previous query searches for documents in the array
collection where the items
array field has exactly three
elements, and then returns only the last
element of the items
array in the query result.
(3) To retrieve a subset of elements from an array field starting from a specific index.
db.array.find({items:{$size:3}}, {items: {$slice:[1,2]}})
The [1,2]
argument to $slice
indicates that the slice should skip the first element of the array and return the next two elements.
✔️ How To Add and Remove Array Elements in MongoDB Documents
✔️ How To Update Array Elements in MongoDB Documents
🕴️ Linkedin: Dragon Slayer 🐲
📝 Articles: All Articles written by D.S