
- 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
Creating alias in a MongoDB query?
You can use aggregate framework to create an alias. Let us first create a collection with documents −
> db.creatingAliasDemo.insertOne({_id:101,"Name":"John Doe"}); { "acknowledged" : true, "insertedId" : 101 } > db.creatingAliasDemo.insertOne({_id:102,"Name":"David Miller"}); { "acknowledged" : true, "insertedId" : 102 } > db.creatingAliasDemo.insertOne({_id:103,"Name":"Sam Williams"}); { "acknowledged" : true, "insertedId" : 103 }
Following is the query to display all documents from the collection with the help of find() method −
> db.creatingAliasDemo.find().pretty();
This will produce the following output −
{ "_id" : 101, "Name" : "John Doe" } { "_id" : 102, "Name" : "David Miller" } { "_id" : 103, "Name" : "Sam Williams" }
Following is the query to create alias in a query −
> db.creatingAliasDemo.aggregate( ... [ ... { ... $project: { ... _id:1, ... "FullName":"$Name" ... } ... } ... ] ... );
This will produce the following output −
{ "_id" : 101, "FullName" : "John Doe" } { "_id" : 102, "FullName" : "David Miller" } { "_id" : 103, "FullName" : "Sam Williams" }
- Related Articles
- Is there a way to create a MySQL “alias” while creating a VIEW?
- How to use alias in MySQL select query?
- Creating views in MongoDB
- How to alias a table in Laravel Eloquent queries using Query Builder?
- Creating hierarchical JSON in MongoDB?
- Set 'alias' for all the column names in a single MySQL query
- Creating an index on a nested MongoDB field?
- “Toggle” query in MongoDB?
- MongoDB query for a single field
- Query MongoDB for a nested search
- MongoDB query to rename a collection?
- Update multiple rows in a single MongoDB query?
- Understanding MongoDB Query Plan
- MongoDB - Query embedded documents?
- How do I query a MongoDB collection?

Advertisements