
- 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
How to add a field with static value to MongoDB find query?
You can use $literal operator along with aggregate framework. Let us first create a collection with documents −
> db.fieldWithStaticValue.insertOne({"Name":"Larry","Age":24}); { "acknowledged" : true, "insertedId" : ObjectId("5cd6554c7924bb85b3f48948") } > db.fieldWithStaticValue.insertOne({"Name":"Chris","Age":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cd655567924bb85b3f48949") } > db.fieldWithStaticValue.insertOne({"Name":"David","Age":26}); { "acknowledged" : true, "insertedId" : ObjectId("5cd655607924bb85b3f4894a") }
Following is the query to display all documents from a collection with the help of find() method −
> db.fieldWithStaticValue.find();
This will produce the following output −
{ "_id" : ObjectId("5cd6554c7924bb85b3f48948"), "Name" : "Larry", "Age" : 24 } { "_id" : ObjectId("5cd655567924bb85b3f48949"), "Name" : "Chris", "Age" : 23 } { "_id" : ObjectId("5cd655607924bb85b3f4894a"), "Name" : "David", "Age" : 26 }
Following is the query to add field with static value to MongoDB using $literal −
> db.fieldWithStaticValue.aggregate( [ { $project: { Name: 1,Age:1, "StaticValue": { $literal: 100 } } } ]);
This will produce the following output −
{ "_id" : ObjectId("5cd6554c7924bb85b3f48948"), "Name" : "Larry", "Age" : 24, "StaticValue" : 100 } { "_id" : ObjectId("5cd655567924bb85b3f48949"), "Name" : "Chris", "Age" : 23, "StaticValue" : 100 } { "_id" : ObjectId("5cd655607924bb85b3f4894a"), "Name" : "David", "Age" : 26, "StaticValue" : 100 }
- Related Articles
- How to add static value while INSERT INTO with SELECT in a MySQL query?
- How to run MongoDB query to update only a specific field value?
- MongoDB query (aggregation framework) to match a specific field value
- MongoDB query to update array with another field?
- How to query MongoDB a value with $lte and $in?
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- MongoDB query to add up the values of a specific field in documents
- MongoDB query to replace value with aggregation?
- How to query on list field in MongoDB?
- MongoDB query to exclude if id is equal to a document field array value
- How to add a day to datetime field in MySQL query?
- MongoDB query to find value in array with multiple criteria (range)
- How to trim spaces from field in MongoDB query?
- How to query MongoDB with a LIMIT?
- MongoDB query to find a value from JSON like data?

Advertisements