
- 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
MongoDB query to add a new field and concatenate the price result divided by a specific number in it
To add a new field, use the $addFields in MongoDB. Let us create a collection with documents −
> db.demo719.insertOne( ... { ... "Number":"7374644", ... "details" : { ... "otherDetails" : [ ... { ... "ProductId" :"102", ... "ProductPrice" : NumberInt(500) ... }, ... { ... "ProductId" :"103", ... "ProductPrice" : NumberInt(2000) ... } ... ] ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5eaae56c43417811278f5882") }
Display all documents from a collection with the help of find() method −
> db.demo719.find();
This will produce the following output −
{ "_id" : ObjectId("5eaae56c43417811278f5882"), "Number" : "7374644", "details" : { "otherDetails" : [ { "ProductId" : "102", "ProductPrice" : 500 }, { "ProductId" : "103", "ProductPrice" : 2000 } ] } }
Following is the query to add a new field and concatenate the price result divided by a specific number in it −
> db.demo719.aggregate([ ... { ... $addFields:{ ... productPriceList: { ... $reduce: { ... input: { ... $map: { ... input: "$details.otherDetails.ProductPrice", ... in: { $toString: { $divide: ["$$this", 5] } } ... } ... }, ... initialValue: "", ... in: { $concat: ["$$value", "$$this", "
"] } ... } ... } ... } ... }, ... { ... $project: { ... _id: 0, ... Number:1, ... productPriceList:1 ... } ... } ... ])
This will produce the following output −
{ "Number" : "7374644", "productPriceList" : "100
400
" }
- Related Articles
- MongoDB query to add up the values of a specific field in documents
- “Structured” grouping query in MongoDB to display result with a new field displaying the count
- MongoDB query to create new field and count set the count of another field in it?
- MongoDB query to count the number of array items in documents and display in a new field
- Add new field to every document in a MongoDB collection?
- MongoDB query (aggregation framework) to match a specific field value
- MongoDB query select and display only a specific field from the document?
- How to add a new field to all the documents in a MongoDB collection
- How to run MongoDB query to update only a specific field value?
- MongoDB query to get a specific number of items
- Search by specific field in MongoDB
- MongoDB query to add new array element in document
- MySQL SELECT to add a new column to a query and give it a value?
- How to add a field with static value to MongoDB find query?
- MongoDB query by sub-field?

Advertisements