
- 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 perform $gt on a hash in a MongoDB document?
The $gt is for greater than, wherein select those documents where the value of the field is greater than the specified value. Let us first create a collection with documents −
> db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":1000,"PlayerLevel":2},"PlayerName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7eba41a844af18acdffa9") } > db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":0,"PlayerLevel":1},"PlayerName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ebbb1a844af18acdffaa") } > db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":-10,"PlayerLevel":0},"PlayerName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ebd41a844af18acdffab") } > db.performQueryDemo.insertOne({"PlayerDetails":{"PlayerScore":1,"PlayerLevel":1},"PlayerName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7ebe31a844af18acdffac") }
Following is the query to display all documents from a collection with the help of find() method −
> db.performQueryDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd7eba41a844af18acdffa9"), "PlayerDetails" : { "PlayerScore" : 1000, "PlayerLevel" : 2 }, "PlayerName" : "Chris" } { "_id" : ObjectId("5cd7ebbb1a844af18acdffaa"), "PlayerDetails" : { "PlayerScore" : 0, "PlayerLevel" : 1 }, "PlayerName" : "Robert" } { "_id" : ObjectId("5cd7ebd41a844af18acdffab"), "PlayerDetails" : { "PlayerScore" : -10, "PlayerLevel" : 0 }, "PlayerName" : "Larry" } { "_id" : ObjectId("5cd7ebe31a844af18acdffac"), "PlayerDetails" : { "PlayerScore" : 1, "PlayerLevel" : 1 }, "PlayerName" : "David" }
Following is the query to perform $gt on a hash in a MongoDB document. Here, we will get the records of the players with score more than 0 −
> db.performQueryDemo.find({"PlayerDetails.PlayerScore":{$gt:0}});
This will produce the following output −
{ "_id" : ObjectId("5cd7eba41a844af18acdffa9"), "PlayerDetails" : { "PlayerScore" : 1000, "PlayerLevel" : 2 }, "PlayerName" : "Chris" } { "_id" : ObjectId("5cd7ebe31a844af18acdffac"), "PlayerDetails" : { "PlayerScore" : 1, "PlayerLevel" : 1 }, "PlayerName" : "David" }
- Related Articles
- Perform nested document value search in MongoDB?
- How to delete a document in MongoDB?
- How to add a sub-document to sub-document array in MongoDB?
- How to get embedded data in a MongoDB document?
- How to insert a document with date in MongoDB?
- How to delete a MongoDB document using Java?
- How to delete particular data in a document in MongoDB?
- Find inside a hash MongoDB?
- How to remove document on the basis of _id in MongoDB?
- How to count number of keys in a MongoDB document?
- How to remove a field completely from a MongoDB document?
- How to update the _id of a MongoDB Document?
- How to sum every field in a sub document of MongoDB?
- How to find a certain element in the MongoDB embedded document?
- How to reach subdata in MongoDB and display a particular document?

Advertisements