Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

MongoDB Sample operators

25 April 2022

Preamble

SQLS*Plus - 165004322953452CCE373 0B83 4AA4 95E1 2B899BA43E76 optimize

​​

MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.

Comparison Query Operators

  • $eq (equal)
  • $ne (not equal)
  • $gt (more than)
  • $lt (less than)
  • $gte (more or equal)
  • $lte (less or equal)
  • $in defines an array of values, one of which should have a document field
  • $nin defines an array of values that should not have a document field

For example, we will find all documents that have the age key value less than 30:

db.users.find ({age: {$lt : 30}})

The use of other comparison operators will be similar. For example, the same key, only over 30:

db.users.find ({age: {$gt : 30}})

Note that the comparison here is made over integer types, not strings. If the key age represents string values then the comparison should be done over the lines: db.users.find ({age: {$gt : “30”}}), but the result will be the same.

But let’s imagine a situation where we have to find all the volumes with an age field value greater than 30 but less than 50. In this case, we can combine two operators:

db.users.find ({age: {$gt : 30, $lt: 50}})

We will find users whose age is 22:

db.users.find ({age: {$eq : 22}})

In essence, this is the analogy of the next query:

db.users.find ({age: 22})

Reverse operation – find users whose age is NOT 22:

db.users.find ({age: {$ne : 22}})

The $in operator defines an array of possible expressions and searches for those keys whose value is in the array:

db.users.find ({age: {$in : [22, 32]}})

In the opposite way, the $nin operator defines an array of possible expressions and searches for those keys whose value is absent in this array:

db.users.find ({age: {$nin : [22, 32]}})

Logical operators

Logical operators are executed over sampling conditions:

  • $or: connects two conditions, and the document must meet one of these conditions
  • $and: connects two conditions, and the document must meet both conditions
  • $not: the document must NOT match the condition
  • $nor: connects two conditions, and the document must NOT meet both conditions

MongoDB Sample operators

The $or operator represents a logical OR operation and defines a set of key-value pairs that should be present in a document. And if a document has at least one such key-value pair, it corresponds to this query and is extracted from the database:

db.users.find ({$or : [{name: "Tom"}, {age: 22}]})

This expression will return all documents with either name=Tom or age=22.

Another example will return all documents with either name=Tom and age=22 or with “german” among the language values:

db.users.find ({name: "Tom", $or : [{age: 22}, {languages: "german"}]})

Conditional operators may be used in subexpressions or subexpressions:

db.users.find ({$or : [{name: "Tom"}, {age: {$gte:30}}]})

In this case we select all documents where name=”Tom” or the age field has a value of 30 or higher.

Operator $and

The $and operator represents a logical operation AND (logical multiplication) and defines a set of criteria that a document must meet. Unlike the $or operator, the document must meet all the specified criteria. For example:

db.users.find ({$and : [{name: "Tom"}, {age: 32}]})

Here the selected documents must have the name Tom and age 32 – both of these features.

MongoDB Array search

Some operators are designed to work with arrays:

  • $all: defines a set of values that should exist in an array
  • $size: defines the number of elements that should be in an array
  • $elemMatch: specifies the condition to which the elements in the array must correspond

MongoDB $all

The $all operator defines an array of possible expressions and requires that documents have the entire defined set of expressions. Accordingly, it is used to search the array. For example, documents have an array of languages that stores the foreign languages spoken by the user. And to find all the people who speak both English and French at the same time, we can use the following expression:

db.users.find ({languages: {$all : ["english", "french"]}})

Operator $elemMatch

The $elemMatch operator allows you to select documents in which arrays contain elements that fall under certain conditions. For example, let the database contain a collection of user ratings for specific courses. Let us add a few documents:

db.grades.insertMany([{student: "Tom", courses:[{name: "Java", grade: 5}, {name: "MongoDB", grade: 4}]},
{student: "Alice", courses:[{name: "C++", grade: 3}, {name: "MongoDB", grade: 5}]}))

Each document has an array, which in turn consists of nested documents.

Now we will find students who have a grade above 3 for the MongoDB course:

db.grades.find({courses: {$elemMatch: {name: "MongoDB", grade: {$gt: 3}}}})

Operator $size

The $size operator is used to find documents in which arrays have a number of elements equal to the value of $size. For example, let’s extract all documents where there are two elements in the laguages array:

db.users.find ({languages: {$size:2}})

Such a query will correspond, for example, to the following document:

{"name": "Tom", "age": 32, languages: ["english", "german"]}

Operator $exists

The $exists operator allows to extract only those documents where a certain key is present or absent. For example, return all the documents that contain the company key:

db.users.find ({company: {$exists:true}})

If we specify $exists as the false parameter, the query will return us only those documents which do not contain the company key.

Operator $type

The $type operator extracts only those documents where a certain key has a value of a certain type, e.g., a string or a number:

db.users.find ({age: {$type: "string"}})
> db.users.find ({age: {$type: "number"}})

Operator $regex

The $regex operator specifies a regular expression to which the field value should correspond. For example, let the field name necessarily have the letter “b”:

db.users.find ({name: {$regex: "b"}})

It is important to understand that $regex does not take just strings, but regular expressions, for example: name: {$regex: “om$”} – the value of name must end with “om”.

MongoDB Tutorial: Array Query Operators ($all, $elemMatch, $size)

Enteros

About Enteros

IT organizations routinely spend days and weeks troubleshooting production database performance issues across multitudes of critical business systems. Fast and reliable resolution of database performance problems by Enteros enables businesses to generate and save millions of direct revenue, minimize waste of employees’ productivity, reduce the number of licenses, servers, and cloud resources and maximize the productivity of the application, database, and IT operations teams.

 
Tags: , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...

Preamble​​When administering PostgreSQL database servers, one of the most common tasks you will probably perform is enumerating databases and their tables....