
- 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
Query MongoDB with length criteria?
To query MongoDB with length criteria, you can use regex. Following is the syntax
db.yourCollectionName.find({ ‘yourFieldName’: { $regex: /^.{yourLengthValue1,yourLengthValue2}$/ } });
Let us create a collection with documents. Following is the query
> db.queryLengthDemo.insertOne({"StudentFullName":"John Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a01ae353decbc2fc927c0") } > db.queryLengthDemo.insertOne({"StudentFullName":"John Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a01b4353decbc2fc927c1") } > db.queryLengthDemo.insertOne({"StudentFullName":"David Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a01c2353decbc2fc927c2") } > db.queryLengthDemo.insertOne({"StudentFullName":"Robert Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a01e2353decbc2fc927c3") } > db.queryLengthDemo.insertOne({"StudentFullName":"Chris Williams"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a01f1353decbc2fc927c4") }
Following is the query to display all documents from a collection with the help of find() method
> db.queryLengthDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9a01ae353decbc2fc927c0"), "StudentFullName" : "John Smith" } { "_id" : ObjectId("5c9a01b4353decbc2fc927c1"), "StudentFullName" : "John Doe" } { "_id" : ObjectId("5c9a01c2353decbc2fc927c2"), "StudentFullName" : "David Miller" } { "_id" : ObjectId("5c9a01e2353decbc2fc927c3"), "StudentFullName" : "Robert Taylor" } { "_id" : ObjectId("5c9a01f1353decbc2fc927c4"), "StudentFullName" : "Chris Williams" }
Following is the query in MongoDB with length criteria
> db.queryLengthDemo.find({ StudentFullName: { $regex: /^.{9,12}$/ } }).pretty();
This will produce the following output
{ "_id" : ObjectId("5c9a01ae353decbc2fc927c0"), "StudentFullName" : "John Smith" } { "_id" : ObjectId("5c9a01c2353decbc2fc927c2"), "StudentFullName" : "David Miller" }
- Related Articles
- MongoDB query to find and return subdocument with criteria?
- MongoDB query to find value in array with multiple criteria (range)
- MongoDB query to count records on the basis of matching criteria
- Match multiple criteria inside an array with MongoDB?
- Find value in a MongoDB Array with multiple criteria?
- Update the last row with search criteria in MongoDB?
- How to match multiple criteria inside an array with MongoDB?
- MongoDB query to find documents having two values in an array conforming to multiple criteria?
- Query MongoDB collection starting with _?
- Create array with MongoDB query?
- How to query MongoDB with “like”?
- MongoDB query with $all in array
- MongoDB query with case insensitive search?
- MongoDB query to replace value with aggregation?
- Query array of nested string with MongoDB?

Advertisements