EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Array
 

MongoDB Array

Priya Pedamkar
Article byPriya Pedamkar

Updated February 28, 2023

MongoDB Array

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

emp count collection

Example #2

Code:

db.emp_count.insertOne({"emp_name":"ABC", "emp_skills": [["PostgreSQL", "MongoDB", "MySQL", "Perl","ORACLE"]]});

Output:

emp skills of employees

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:

multiple array field

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.

count collection

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" } )

single field in MongoDB

db.emp_count.find ( { emp_address: "Delhi" } )

MongoDB Array6

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 ()

Stud Test Table

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:

all array operator

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:

elemMatch array operator

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:

size array operator

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:

MongoDB Array11

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:

pop array operator

db.stud_test.find()

stud test

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:

pull array operator

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:

push array operator

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 –

  1. Lookup in MongoDB
  2. MongoDB Tools
  3. MongoDB Delete | How to Work?
  4. MongoDB Array | Examples
  5. Guide to MongoDB Distinct
  6. How to Check MongoDB Versions?
  7. MongoDB Aggregate | How to Work?

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW