MongoDB string field value length query greater than or less than
Today this article will see how to write a MongoDB string field value length query greater than or less than.
Today in this article, we will cover below aspects,
We will cover below query types,
- Greater than field value length
- Less than field value length
- Equal to the length of the field value
We shall see examples for MongoDB field query where we will get the records based on field length value greater than or less than of specified value.
We will run field-length queries using CLI or Compass UI or Mongo shell.
We already looked at a simple way of adding or updating a new field to the document in our previous MongoDB sample series.
We shall also see how to create a similar query using another language like C# Mongo driver.
Getting started
Here below is a sample schema or document we shall use for field length query and will cover less than or greater than length query for field value “ZIP”.
Mongo field length query using CLI or Compass UI
Here we will be using the below query to get the documents based on the value of the length of fields in the MongoDB Collection.
Pattern:
{"<Field>": { "$exists": true },"$expr": { "$gt": [ { "$strLenCP":"$<Field>"}, <Field Length> ]}}
Where <Field> is the Mongo Field Name whos value length needs to be calculated. Example: "ZipCode"
MongoDB string field value length greater than query >
Below is the MongoDB string field value length greater than the query.
Query Pattern
db.collection.find({"<Field>": { "$exists": true },"$expr": { "$gt": [ { "$strLenCP":"$<Field>"}, <Field Length> ]}})
Example
In the below query, we get records where the field exists and the field length is greater than 5 in size.
{"Zip": { "$exists": true },"$expr": { "$gt": [ { "$strLenCP":"$Zip" }, 5]}}
MongoDB string field value length less than query <
Below is the Mongo Shell query for less than dates.
Query Pattern
db.collection.find({"<Field>": { "$exists": true },"$expr": { "$lt": [ { "$strLenCP":"$<Field>"}, <Field Length> ]}})
Example
In the below query, we get records where the field exists and the field length is less than 5 in size.
{"Zip": { "$exists": true },"$expr": { "$lt": [ { "$strLenCP":"$Zip" }, 5]}}
MongoDB string field value length Equal to length =
Query Pattern
db.collection.find({"<Field>": { "$exists": true },"$expr": { "$eq: [ { "$strLenCP":"$<Field>"}, <FieldLength> ]}})
Example
In the below query, we get records where the field exists and the field length is equal to the size specified which is 5.
{"Zip": { "$exists": true },"$expr": { "$eq": [ { "$strLenCP":"$Zip" }, 5]}}
What is the difference between gt and gte or lt and lte in MongoDB?
The above commands are very simple and easy to understand. There are small differences between these commands.
gte
gte = greater than or equal to i.e > =
gt
gt = greater than i.e >
lte
lte = less than or equal to i.e < =
lt
lt = less than i.e <
Kindly visit the below article for all examples using C# MongoDB driver,
References:
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.