
- 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
Project field in MongoDB
To project field in MongoDB, use $project. Let us create a collection with documents −
> db.demo439.insertOne( ... { ... "Name" : "Chris", ... "MarksInformation" : { ... "Marks1" : 67, ... "Marks2" :45, ... "Marks3" : 78 ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e77833abbc41e36cc3caeab") } > db.demo439.insertOne( ... { ... "Name" : "David", ... "MarksInformation" : { ... "Marks1" : 50, ... "Marks2" :57, ... "Marks3" : 68 ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e77833abbc41e36cc3caeac") } > db.demo439.insertOne( ... { ... "Name" : "Bob", ... "MarksInformation" : { ... "Score1" : 65, ... "Score2" :71, ... ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e77833bbbc41e36cc3caead") }
Display all documents from a collection with the help of find() method −
> db.demo439.find();
This will produce the following output −
{ "_id" : ObjectId("5e77833abbc41e36cc3caeab"), "Name" : "Chris", "MarksInformation" : { "Marks1" : 67, "Marks2" : 45, "Marks3" : 78 } } { "_id" : ObjectId("5e77833abbc41e36cc3caeac"), "Name" : "David", "MarksInformation" : { "Marks1" : 50, "Marks2" : 57, "Marks3" : 68 } } { "_id" : ObjectId("5e77833bbbc41e36cc3caead"), "Name" : "Bob", "MarksInformation" : { "Score1" : 65, "Score2" : 71 } }
Following is the query to project field −
> db.demo439.aggregate({ ... $project: { ... "Name" : 1, ... "Marks1": { $cond: [ { $eq:[ { $ifNull: [ "$MarksInformation.Marks1", 0 ] }, 0 ] },{ $ifNull: [ "$MarksInformation.Score1", 0 ] }, "$MarksInformation.Marks1" ] }, ... "Marks2": { $cond: [ { $eq:[ { $ifNull: [ "$MarksInformation.Marks2", 0 ] }, 0 ] }, { $ifNull: [ "$MarksInformation.Score2", 0 ] }, "$MarksInformation.Marks2" ] }, ... "Marks3": { $cond: [ { $eq:[ { $ifNull: [ "$MarksInformation.Marks3", 0] }, 0 ] }, { $ifNull: [ "$MarksInformation.Score3", 0 ] }, "$MarksInformation.Marks3" ] } ... }})
This will produce the following output −
{ "_id" : ObjectId("5e77833abbc41e36cc3caeab"), "Name" : "Chris", "Marks1" : 67, "Marks2" : 45, "Marks3" : 78 } { "_id" : ObjectId("5e77833abbc41e36cc3caeac"), "Name" : "David", "Marks1" : 50, "Marks2" : 57, "Marks3" : 68 } { "_id" : ObjectId("5e77833bbbc41e36cc3caead"), "Name" : "Bob", "Marks1" : 65, "Marks2" : 71, "Marks3" : 0 }
- Related Articles
- Project specific array field in a MongoDB collection?
- Working with MongoDB $concatArrays in $project on existing multi-array field
- How to project specific elements in an array field with MongoDB?
- How to project grouping into object in MongoDB and display only the marks field?
- How to display a specific field in array using $project in MongoDB and ignore other fields?
- Reverse array field in MongoDB?
- Update _id field in MongoDB
- Hide id field in MongoDB
- How to Stay Popular in Project Management Field?
- Implement $match and $project in MongoDB aggregate
- Return a specific field in MongoDB?
- Particular field as result in MongoDB?
- Fetch specific field values in MongoDB
- Search by specific field in MongoDB
- MongoDB slice array in populated field?

Advertisements