

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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 write not greater than in a MySQL query?
- Check which element in a masked array is greater than a given value
- 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?
- MongoDB query where all array items are greater than a specified condition?
- Mask array elements greater than or equal to a given value in Numpy
- How do I remove a string from an array in a MongoDB document?
- How to get a JSON array field in a nested JSON using Rest Assured?
- How to select where sum of fields is greater than a value in MongoDB?
- 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?
- Find Strings greater than a particular length in MongoDB?
Advertisements