$and operator: MongoDB

Lets quickly learn about using $and operator in MongoDB.

Related Read:
$exists, $type, $regex operators: MongoDB
$or (Union) Operator: MongoDB

Documents in our collection
test database, names collection.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> db.names.find().pretty()
{ "_id" : ObjectId("53be5d4604cc1cb0a7bfc3c0"), "name" : "Alia" }
{ "_id" : ObjectId("53be5d5204cc1cb0a7bfc3c1"), "name" : "Bebo" }
{ "_id" : ObjectId("53be5d5904cc1cb0a7bfc3c2"), "name" : "Chameli" }
{ "_id" : ObjectId("53be5d6104cc1cb0a7bfc3c3"), "name" : "Dev D" }
{ "_id" : ObjectId("53be5d6804cc1cb0a7bfc3c4"), "name" : "Emli" }
{ "_id" : ObjectId("53be5d8604cc1cb0a7bfc3c5"), "name" : "Farhan" }
{ "_id" : ObjectId("53be5d9204cc1cb0a7bfc3c6"), "name" : "Gangs" }
{ "_id" : ObjectId("53be5d9904cc1cb0a7bfc3c7"), "name" : "Hum" }
{ "_id" : ObjectId("53be5e3704cc1cb0a7bfc3c8"), "name" : 25 }
{
        "_id" : ObjectId("53beaa0f6a8a31dc255d4589"),
        "name" : "Satish",
        "age" : 27
}

$and operator: MongoDB


[youtube https://www.youtube.com/watch?v=Ur0Mmj7yEOI]

YouTube Link: https://www.youtube.com/watch?v=Ur0Mmj7yEOI [Watch the Video In Full Screen.]



1
2
3
> db.names.find({$and: [{"name": {$regex: "e"}}, {"name": {$gt: "C"}}]});
{ "_id" : ObjectId("53be5d5904cc1cb0a7bfc3c2"), "name" : "Chameli" }
{ "_id" : ObjectId("53be5d6104cc1cb0a7bfc3c3"), "name" : "Dev D" }

As you can see, $and works/outputs results only when all the conditions in the array is met. i.e., in above command, the name must have small letter “e” in it and must be lexicographically greater than capital letter “C”.

$or (Union) Operator: MongoDB

Today lets learn about $or operator.

In set theory, the union (denoted by ∪) of a collection of sets is the set of all distinct elements in the collection. It is one of the fundamental operations through which sets can be combined and related to each other.

Union of two sets
The union of two sets A and B is the collection of points which are in A or in B or in both A and B. In symbols,

union-of-two-sets
For example, if A = {1, 3, 5, 7} and B = {1, 2, 4, 6} then A ∪ B = {1, 2, 3, 4, 5, 6, 7}.

$or (Union) Operator: MongoDB


[youtube https://www.youtube.com/watch?v=84GxdweNoCQ]

YouTube Link: https://www.youtube.com/watch?v=84GxdweNoCQ [Watch the Video In Full Screen.]



Documents in our collection
test database, names collection.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> db.names.find().pretty()
{ "_id" : ObjectId("53be5d4604cc1cb0a7bfc3c0"), "name" : "Alia" }
{ "_id" : ObjectId("53be5d5204cc1cb0a7bfc3c1"), "name" : "Bebo" }
{ "_id" : ObjectId("53be5d5904cc1cb0a7bfc3c2"), "name" : "Chameli" }
{ "_id" : ObjectId("53be5d6104cc1cb0a7bfc3c3"), "name" : "Dev D" }
{ "_id" : ObjectId("53be5d6804cc1cb0a7bfc3c4"), "name" : "Emli" }
{ "_id" : ObjectId("53be5d8604cc1cb0a7bfc3c5"), "name" : "Farhan" }
{ "_id" : ObjectId("53be5d9204cc1cb0a7bfc3c6"), "name" : "Gangs" }
{ "_id" : ObjectId("53be5d9904cc1cb0a7bfc3c7"), "name" : "Hum" }
{ "_id" : ObjectId("53be5e3704cc1cb0a7bfc3c8"), "name" : 25 }
{
        "_id" : ObjectId("53beaa0f6a8a31dc255d4589"),
        "name" : "Satish",
        "age" : 27
}

Related Read: $exists, $type, $regex operators: MongoDB

1
2
3
> db.names.find({$or: [{"name": {$regex: "^E"}}, {"age": {$exists: true}}]});
{ "_id" : ObjectId("53be5d6804cc1cb0a7bfc3c4"), "name" : "Emli" }
{ "_id" : ObjectId("53beaa0f6a8a31dc255d4589"), "name" : "Satish", "age" : 27 }

$or is a prefix operator. It takes array as it’s value. The array can contain any number of objects for the union. Mongo Shell fetches all the documents which matches any of the documents which the individual objects inside the array points to.

1
2
3
> db.names.find({$or: [{"name": {$regex: "^E"}}, {"name": {$type: 1}}]});
{ "_id" : ObjectId("53be5d6804cc1cb0a7bfc3c4"), "name" : "Emli" }
{ "_id" : ObjectId("53be5e3704cc1cb0a7bfc3c8"), "name" : 25 }

$type: 1, points to Boolean value.

Note: Next we’ll learn how to make use of $and operator.