Updated February 28, 2023
Introduction to MongoDB Array
MongoDB Array is a flexible document structure; it will make it possible to have a field with array as a value in MongoDB. This is nothing but a simple list of values, and it can take many forms in MongoDB. We can define an array of string, integer, embedded documents, Jason and BSON data types, array in it can be defined as any form of data types. If we have made student collection and make the field as grade and grade are divided into three parts, such as MongoDB array, Array is essential and useful in MongoDB.
Syntax
We can define arrays are as follows.
{< array field >: {< operator1> : <value1>, < operator2> : <value2>, < operator3> : <value3>, ….. }}
Parameters to MongoDB Array
1. Array field: Array field is defined as the field name of the collection on which we create or define array values. The array field is significant while defining any array.
2. Operator: The operator is defined as which values we have to create the array. The operator name is specific to the value name in the array.
3. Value: Value in the array is defined as the actual value of array on which we have defining array in MongoDB. Value is significant while defining an array.
Examples to Implement MongoDB Array
Here are some examples for better understanding:
Example #1
In the below example, we have to define an array of emp_skills after defining we have inserted an array of documents in emp_count collection:
Code:
db.emp_count.find()
Output:
Example #2
Code:
db.emp_count.insertOne({"emp_name":"ABC", "emp_skills": [["PostgreSQL", "MongoDB", "MySQL", "Perl","ORACLE"]]});
Output:
Explanation: In the above example, we have to define an array of emp_skills of employees. At the time of defining an array, we have inserted documents and created emp_count collection. In the above example, we have created an array of emp_skills in emp_count collection. At the time of defining the array, we have created a collection name as emp_array. We can define an array at the time of insertion. We can define multiple array fields in a single collection.
Example #3
Below example shows that define a multiple array field in the single collection are as follows:
Code:
db.emp_countmultiple.insertOne({"emp_name":"ABC", "emp_skills":[["PostgreSQL", "MongoDB", "MySQL", "Perl", "ORACLE"]], "emp_address":[["Pune", "Mumbai", "Delhi"]]});
Output:
Explanation: In the above example, we have defined multiple array fields in one collection. We have to define emp_skills and emp_address array in a single collection.
How to Initialize Array in MongoDB?
Below is the description of how to initialize an array in MongoDB.
We have taken an example of emp_count collection to describe how to initialize an array in MongoDB.
We can initialize the whole array in a single field in MongoDB. The below example shows the initialization of the array using a single field is as follows.
db.emp_count.find ( { emp_address: "Pune" } )
db.emp_count.find ( { emp_address: "Delhi" } )
Explanation: In the above example, we have initialized array using a single field. In the first example, we have used field tags as red while using tags as red four documents from emp_count collection are displayed or initialized. In this example, we have initialized array-based in emp_address values at that time only matched record will be displayed or initialized.
Various Array Operators in MongoDB
Below are the types of array Operators available in MongoDB.
- $all
- $elemMatch
- $size
- $
- $pull
- $push
- $pop
We have taken an example of the stud_test table to describe the various operator’s example as follows.
db.stud_test.find ()
1. $all
$all array operator is used to display all the value from the array field. Below example show $all array operator:
Code:
db.stud_test.find ({results: {$all: [88]}})
Output:
2. $elemMatch
$elemMatch array operator is used to match the document which contains the array field and contains only one filed to match our given criteria:
Code:
db.stud_test.find({results: {$elemMatch: {$gte: 80, $lt: 95}}})
Output:
3. $size
The size array operator in MongoDB will match any array with the number of elements specified by the argument. The below example shows a $size array operator:
Code:
db.stud_test.find ({“results": {$size: 3}});
Output:
4. $
The below example shows an $ array operator. $ Array operator is used to identify array element and update the same into the collection:
Code:
db.stud_test.updateOne ({stud_id: 1, results: 85},{$set: {"results.$" : 95 }})
Output:
5. $pop
$pop array operator is used to remove the first and last element from an array. The below example shows a $pop array operator:
Code:
db.stud_test.updateOne( { stud_id: 1 }, { $pop: { results: -1 } } )
Output:
db.stud_test.find()
6. $pull
A pull array operator is used to remove elements from an existing array. The below example shows a $pull array operator are as follows:
Code:
db.stud_test.update( { stud_id: 1 }, { $pull: { results: { $gte: 95 } } } )
Output:
7. $push
A push array operator is used to append the value into the existing collection. The below example shows the push array operator:
Code:
db.stud_test.update ({stud_id: 1}, { $push: { results: 101 }})
Output:
Conclusion
The array is significant and useful in MongoDB to define a set of elements in a single set. Different types of array operator available in MongoDB like $, $size, $elemMatch, $pop, $all, $push and $pull.MongoDB array is nothing but a simple list of values in MongoDB.
Recommended Articles
This is a guide to MongoDB Array. Here we discuss MongoDB Array’s syntax, parameters, examples, and how to initialize it with various Operators. You can also go through our other related articles to learn more –