Discussing about Pipeline Expression in MongoDB

In the previous article we scratched the surface of the expression in pipeline. In this article we are going to discuss it in detail.

What is the pipeline expression in mongodb?

As the mongodb.org states pipeline expression in mongodb is

Some pipeline stages takes a pipeline expression as its operand. Pipeline expressions specify the transformation to apply to the input documents. Expressions have a document structure and can contain other expression.

How do we use pipeline expression in mongodb?

In pipeline expressions field paths and system variables, literals, expression objects, and expression operators can be included by expressions. They can also be nested. Aggregation expressions use field path to access fields in the input documents.
To specify a field path, we can use a string that prefixes with a dollar sign , the field name or the dotted field name, if the field is in embedded document. For example, “$user” to specify the field path for the user field or “$user.name” to specify the field path to “user.name” field. “$<field>” is equivalent to “$$CURRENT.<field>” where the CURRENT is a system variable that defaults to the root of the current object in the most stages.

Expression Objects:

If the expressions are numeric or Boolean values. In that case mongodb treats the values as projection flags. Which are valid only in the $project stage. To avoid treating numeric or Boolean values as projection flags, we can use the $literal expression to wrap these values. Following is a basic example of expression objects:


{ <field1>: <expression1>, ... }

Operator Expressions:

Operator expressions are similar to functions that take arguments. In general, these expressions take an array of arguments. As shown in the below:


{ <operator>: [ <argument1>, <argument2> ... ] }

If operator accepts a single argument, we will be able to omit the outer array designating the argument list.


{ <operator>: <argument> }

To avoid ambiguity in parsing we can wrap the literal array with a $literal expression.

Boolean Expressions:

Boolean expressions evaluates its argument expressions as Boolean and return a Boolean as the result. The Boolean expression evaluates all other values as true, including non-zero numeric values and arrays. There are three Boolean expressions. We will discuss about them in the below.

$and:
This expression returns true only when all of its expressions evaluate to true. Accepts any number of argument expressions. This expression uses short-circuit logic. With this logic the operation stops evaluation after encountering the first false expression. So , in case of first expression being false the second expression will not be checked.
Let us view a basic syntax for better understanding:


{ $and: [ <expression1>, <expression2>, ... ] }

As an example let us think of an e-shopping site. where if you want to buy something  the database has to check two things first. At the beginning if the product that you specified exists or not in the inventory. After that is the price of the product is accurate or not. If these two parameters return true, the the procedure will proceed.


db.inventory.find( { product: { $exists: true, $price: 150 } } )

$or:

This expression always returns true when any of its expressions evaluates to true. Accepts any number of argument expressions. This expression also uses short-circuit logic. But it is a little bit different than the previous expression. In here the operation will stop evaluation after encountering the first true expression.
Let us view an basic syntax for better understanding:


{ $or: [ <expression1>, <expression2>, ... ] }

$not:
This expression returns the Boolean value that is the opposite of its argument expression. Accepts a single argument expression. In this case when an expression evaluates to true, $not returns false; when an expression evaluates to false, $not returns true.

Let us view an basic syntax for better understanding:


{ $not: [ <expression> ] }

Suppose this time in that same e-shopping site you want to find a certain item which will not exceed over 2000 in respect of price. To search that particular product you have to give certain parameters. whereas you provide with the maximum amount of the price. in the below code we can see that the program will show only those items, whose price are not greater than 2000.


db.inventory.find( { price: { $not: { $gt: 2000 } } } )

 

Related Links:

1> How to Get Started with MongoDB Database?
2> How to Get Started with MongoDB?
3> How to Import and Export Through Mongodb?
4> How to Use Projection in MongoDB?
5> Using sort method in mongodb
6> Map-Reduce in MongoDB
7> Introduction to Replication in MongoDB
8> Deploying a Replica Set in MongoDB
9> Discussing Replication Lag in MongoDB
10> Replica Set Members in Mongodb
11> Working with Sharding in MongoDB
12> Working with Index in MongoDB
13> Working with Aggregation in MongoDB
14> How to Work with Aggregation Framework in MongoDB?
15> Working with Pipeline Concept in MongoDB

If you find this article helpful, you can connect us in Google+ and Twitter.

1 thought on “Discussing about Pipeline Expression in MongoDB

Leave a Reply

Your email address will not be published. Required fields are marked *