
- 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
Find Strings greater than a particular length in MongoDB?
To find the string which has a length greater than a particular value in MongoDB, use the $where operator. The syntax is as follows −
db.yourCollectionName.find({$where:'this.yourStringFieldName.length > yourIntegerValue'}).pretty();
To understand the above concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.stringFieldLengthDemo.insertOne({"UserId":1,"UserName":"Adam Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77bb4b2386c62d05142a78") } > db.stringFieldLengthDemo.insertOne({"UserId":2,"UserName":"Carol Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77bb562386c62d05142a79") } > db.stringFieldLengthDemo.insertOne({"UserId":3,"UserName":"James Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77bb5b2386c62d05142a7a") } > db.stringFieldLengthDemo.insertOne({"UserId":4,"UserName":"John Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77bb662386c62d05142a7b") } > db.stringFieldLengthDemo.insertOne({"UserId":5,"UserName":"David Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77bb892386c62d05142a7c") } > db.stringFieldLengthDemo.insertOne({"UserId":6,"UserName":"John Williams"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77bbb02386c62d05142a7d") } > db.stringFieldLengthDemo.insertOne({"UserId":7,"UserName":"Chris Evans"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77bbd32386c62d05142a7e") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.stringFieldLengthDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c77bb4b2386c62d05142a78"), "UserId" : 1, "UserName" : "Adam Smith" } { "_id" : ObjectId("5c77bb562386c62d05142a79"), "UserId" : 2, "UserName" : "Carol Taylor" } { "_id" : ObjectId("5c77bb5b2386c62d05142a7a"), "UserId" : 3, "UserName" : "James Brown" } { "_id" : ObjectId("5c77bb662386c62d05142a7b"), "UserId" : 4, "UserName" : "John Smith" } { "_id" : ObjectId("5c77bb892386c62d05142a7c"), "UserId" : 5, "UserName" : "David Miller" } { "_id" : ObjectId("5c77bbb02386c62d05142a7d"), "UserId" : 6, "UserName" : "John Williams" } { "_id" : ObjectId("5c77bbd32386c62d05142a7e"), "UserId" : 7, "UserName" : "Chris Evans" }
Here is the query to find the string which has a length greater than let‟s say 11 in MongoDB −
> db.stringFieldLengthDemo.find({$where:'this.UserName.length >11'}).pretty();
The following is the output −
{ "_id" : ObjectId("5c77bb562386c62d05142a79"), "UserId" : 2, "UserName" : "Carol Taylor" } { "_id" : ObjectId("5c77bb892386c62d05142a7c"), "UserId" : 5, "UserName" : "David Miller" } { "_id" : ObjectId("5c77bbb02386c62d05142a7d"), "UserId" : 6, "UserName" : "John Williams" }
- Related Articles
- Python - Find words greater than given length
- MongoDB query where all array items are greater than a specified condition?
- 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?
- Find Smallest Letter Greater Than Target in Python
- Find smallest element greater than K in Python
- Find Smallest Letter Greater Than Target in JavaScript
- MongoDB query to match documents with array values greater than a specific value
- Find Elements Greater Than a Given Number In a Subarray in Java
- How can I find all columns where the column name length is greater than 5 in MySQL?
- How do I get a value array (instead a json array) greater than 50 in MongoDB?
- How to get values greater than a specific value from an embedded list in MongoDB?
- Display collections in a particular MongoDB database?
- Find documents which contain a particular value in MongoDB using Regular Expressions?
- Match MongoDB documents with field value greater than a specific number and fetch them?

Advertisements