
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
What is the $unwind operator in MongoDB?
The $unwind operator in MongoDB is the same for each array, it returns the mapping document. Here is the demo of $unwind operator 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.unwindOperatorDemo.insertOne({"StudentName":"Larry","StudentAge":23,"StudentSubje ct":["C","C++","Java","MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c7ef5f3559dd2396bcfbfc8") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.unwindOperatorDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c7ef5f3559dd2396bcfbfc8"), "StudentName" : "Larry", "StudentAge" : 23, "StudentSubject" : [ "C", "C++", "Java", "MongoDB" ] }
Here is the demo of $unwind operator. The query is as follows −
> db.unwindOperatorDemo.aggregate( ... { $project : { ... StudentName : 1 , ... StudentAge: 1 , ... StudentSubject : 1 ... }}, ... { $unwind : "$StudentSubject" } ... ).pretty();
The following is the output −
{ "_id" : ObjectId("5c7ef5f3559dd2396bcfbfc8"), "StudentName" : "Larry", "StudentAge" : 23, "StudentSubject" : "C" } { "_id" : ObjectId("5c7ef5f3559dd2396bcfbfc8"), "StudentName" : "Larry", "StudentAge" : 23, "StudentSubject" : "C++" } { "_id" : ObjectId("5c7ef5f3559dd2396bcfbfc8"), "StudentName" : "Larry", "StudentAge" : 23, "StudentSubject" : "Java" } { "_id" : ObjectId("5c7ef5f3559dd2396bcfbfc8"), "StudentName" : "Larry", "StudentAge" : 23, "StudentSubject" : "MongoDB" }
Look at the sample output, the fields “StudentName” and “StudentAge” are mapping to each “StudentSubject” array field.
- Related Articles
- MongoDB $unwind to get the count
- Unwind two arrays from MongoDB
- MongoDB query to unwind two arrays
- Implement MongoDB Aggregate - unwind, group and project?
- What is the ?-->? operator in C++?
- What is the operator in MySQL?
- MongoDB divide aggregation operator?
- What is the conditional operator ?: in Java?
- What is the operator precedence in C#?
- Using positional operator with hierarchies in MongoDB?
- MongoDB pull with positional operator?
- What is a Ternary operator/conditional operator in C#?
- What is @ operator in Python?
- What is the purpose of ‘is’ operator in C#?
- How to query MongoDB using the $ne operator?

Advertisements