- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Fetch records in MongoDB on querying its subset
You can use $all operator. Let us first create a collection with documents −
> db.subsetOfAnArrayDemo.insertOne({"StudentProgrammingSkills": ["Java","MongoDB","MySQL","C++","Data Structure","Algorithm","Python","Oracle","SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cb9d1e1895c4fd159f80804") }
Following is the query to display all documents from the collection with the help of find() method −
> db.subsetOfAnArrayDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cb9d1e1895c4fd159f80804"), "StudentProgrammingSkills" : [ "Java", "MongoDB", "MySQL", "C++", "Data Structure", "Algorithm", "Python", "Oracle", "SQL Server" ] }
Following is the query to get the subset of an array −
> db.subsetOfAnArrayDemo.find({ StudentProgrammingSkills: { $all: [ 'MongoDB', 'MySQL' ] } } ).pretty();
This will produce the following output −
{ "_id" : ObjectId("5cb9d1e1895c4fd159f80804"), "StudentProgrammingSkills" : [ "Java", "MongoDB", "MySQL", "C++", "Data Structure", "Algorithm", "Python", "Oracle", "SQL Server" ] }
- Related Articles
- Using aggregation pipeline to fetch records in MongoDB
- How to use or operator in MongoDB to fetch records on the basis of existence?
- Querying array of Embedded Documents in MongoDB based on Range?
- MongoDB query to fetch date records (ISODate format) in a range
- Querying null value in MongoDB?
- Querying with MongoDB subelement?
- Fetch records on the basis of LastName using MySQL IN()
- Querying internal array size in MongoDB?
- Querying array elements with MongoDB?
- Fetch records from a subdocument array wherein id begins from 234 in MongoDB
- Querying an array of arrays in MongoDB?
- How to improve querying field in MongoDB?
- Querying on an array of objects for specific nested documents with MongoDB?
- Find MongoDB records based on a condition?
- MySQL to fetch records based on a specific month and year?

Advertisements