
- 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 do I get a value array (instead a json array) greater than 50 in MongoDB?
To avoid getting json array and get a value array, use $in. For greater than, use MongoDB $gt. Let us create a collection with documents −
> db.demo50.save({"Value":40}); WriteResult({ "nInserted" : 1 }) > db.demo50.save({"Value":100}); WriteResult({ "nInserted" : 1 }) > db.demo50.save({"Value":20}); WriteResult({ "nInserted" : 1 }) > db.demo50.save({"Value":510}); WriteResult({ "nInserted" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo50.find();
This will produce the following output −
{ "_id" : ObjectId("5e270c02cfb11e5c34d89903"), "Value" : 40 } { "_id" : ObjectId("5e270c05cfb11e5c34d89904"), "Value" : 100 } { "_id" : ObjectId("5e270c07cfb11e5c34d89905"), "Value" : 20 } { "_id" : ObjectId("5e270c11cfb11e5c34d89906"), "Value" : 510 }
Following is the query to get a value array in MongoDB greater than 50 −
> listOfValues = db.demo50.distinct("_id", {Value:{$gt:50}}); [ ObjectId("5e270c05cfb11e5c34d89904"), ObjectId("5e270c11cfb11e5c34d89906") ] > db.demo50.find({_id:{$in:listOfValues}});
This will produce the following output −
{ "_id" : ObjectId("5e270c05cfb11e5c34d89904"), "Value" : 100 } { "_id" : ObjectId("5e270c11cfb11e5c34d89906"), "Value" : 510 }
- Related Articles
- How do I delete array value from a document in MongoDB?
- MongoDB query to match documents with array values greater than a specific value
- Mask array elements greater than a given value in Numpy
- How do I add a value to the top of an array in MongoDB?
- How to get values greater than a specific value from an embedded list in MongoDB?
- Check which element in a masked array is greater than a given value
- MongoDB query where all array items are greater than a specified condition?
- How do I remove a string from an array in a MongoDB document?
- How do I write not greater than in a MySQL query?
- Mask array elements greater than or equal to a given value in Numpy
- How to get a JSON array field in a nested JSON using Rest Assured?
- Query for documents where array size is greater than 1 in MongoDB?
- How to find numbers in an array that are greater than, less than, or equal to a value in java?
- How to select where sum of fields is greater than a value in MongoDB?
- How to get array from a MongoDB collection?

Advertisements