Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Creating views in MongoDB
To create views in MongoDB, use createView(). Let us create a collection with documents −
> db.demo113.insertOne(
... { _id: 1, StudentId: "101", "Details": { Name: "Chris", Age: 21 }, Subject: "MySQL" }
... );
{ "acknowledged" : true, "insertedId" : 1 }
Display all documents from a collection with the help of find() method −
> db.demo113.find().pretty();
This will produce the following output −
{
"_id" : 1,
"StudentId" : "101",
"Details" : {
"Name" : "Chris",
"Age" : 21
},
"Subject" : "MySQL"
}
Following is the query to create views in MongoDB −
> db.createView(
... "firstView",
... "demo113",
... [ { $project: { "Name": "$Details.Name", Subject: 1 } } ]
... )
{ "ok" : 1 }
Display fields from a view with the help of find() method −
> db.firstView.find();
This will produce the following output −
{ "_id" : 1, "Subject" : "MySQL", "Name" : "Chris" }Advertisements