Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Querying an array of arrays in MongoDB?
Use $in operator to query an array of arrays in MongoDB. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.arrayOfArraysDemo.insertOne({"EmployeeName":"Larry","EmployeeSkills":[["Java","MongoDB","MySQL","SQL Server"]]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c7f7a8d8d10a061296a3c5b")
}
> db.arrayOfArraysDemo.insertOne({"EmployeeName":"Mike","EmployeeSkills":[["C","C++"]]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c7f7aa68d10a061296a3c5c")
}
Display all documents from a collection with the help of find() method. The query is as follows −
> db.arrayOfArraysDemo.find().pretty();
The following is the output −
{
"_id" : ObjectId("5c7f7a8d8d10a061296a3c5b"),
"EmployeeName" : "Larry",
"EmployeeSkills" : [
[
"Java",
"MongoDB",
"MySQL",
"SQL Server"
]
]
}
{
"_id" : ObjectId("5c7f7aa68d10a061296a3c5c"),
"EmployeeName" : "Mike",
"EmployeeSkills" : [
[
"C",
"C++"
]
]
}
Here is the query for querying an array of arrays in MongoDB −
> db.arrayOfArraysDemo.find({'EmployeeSkills':{$elemMatch:{$elemMatch:{$in:['MongoDB']}}}
}).pretty();
The following is the output −
{
"_id" : ObjectId("5c7f7a8d8d10a061296a3c5b"),
"EmployeeName" : "Larry",
"EmployeeSkills" : [
[
"Java",
"MongoDB",
"MySQL",
"SQL Server"
]
]
} Advertisements
