
- 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
Get Absolute value with MongoDB aggregation framework?
You can use $abs operator for this. Let us first create a collection with documents
> db.absoluteValueDemo.insert({"Value":98}); WriteResult({ "nInserted" : 1 }) > db.absoluteValueDemo.insert({"Value":-100}); WriteResult({ "nInserted" : 1 }) > db.absoluteValueDemo.insert({"Value":0}); WriteResult({ "nInserted" : 1 }) > db.absoluteValueDemo.insert({"Value":-9999990}); WriteResult({ "nInserted" : 1 })
Following is the query to display all documents from a collection with the help of find() method:
> db.absoluteValueDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5ca271f56304881c5ce84b9a"), "Value" : 98 } { "_id" : ObjectId("5ca271fa6304881c5ce84b9b"), "Value" : -100 } { "_id" : ObjectId("5ca271fe6304881c5ce84b9c"), "Value" : 0 } { "_id" : ObjectId("5ca2720f6304881c5ce84b9d"), "Value" : -9999990 }
Following is the query to get absolute value with MongoDB aggregation framework
> db.absoluteValueDemo.aggregate({ $project: { AbsoluteValue: { $abs: '$Value' } }});
This will produce the following output
{ "_id" : ObjectId("5ca271f56304881c5ce84b9a"), "AbsoluteValue" : 98 } { "_id" : ObjectId("5ca271fa6304881c5ce84b9b"), "AbsoluteValue" : 100 } { "_id" : ObjectId("5ca271fe6304881c5ce84b9c"), "AbsoluteValue" : 0 } { "_id" : ObjectId("5ca2720f6304881c5ce84b9d"), "AbsoluteValue" : 9999990 }
- Related Articles
- MongoDB aggregation framework with group query example?
- MongoDB query (aggregation framework) to match a specific field value
- MongoDB aggregation framework match OR is possible?
- Match between fields in MongoDB aggregation framework?
- Get the average of an entire field using the aggregation framework in MongoDB?
- MongoDB query to replace value with aggregation?
- MongoDB query to group several fields using aggregation framework?
- MongoDB aggregation framework to sort by length of array?
- MongoDB aggregation to fetch documents with specific field value?
- MongoDB aggregation to get two documents with the least marks
- Replace value with a string literal during MongoDB aggregation operation
- MongoDB aggregation with multiple keys
- Is it possible to sum two fields in MongoDB using the Aggregation framework?
- Retrieving an embedded object as a document via the aggregation framework in MongoDB?
- How to exclude _id without including other fields using the aggregation framework in MongoDB?

Advertisements