Dot Notation To Access Sub Document: MongoDB

Today lets see how we can access sub-document or sub-object using Dot notation, in MongoDB.

Documents in our collection
nesting database, users collection.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
> use nesting
switched to db nesting
 
> db.users.insert({"name": "Satish", 
                   "email": {"work": "[email protected]", 
                   "personal": "[email protected]"}});
WriteResult({ "nInserted" : 1 })
 
> db.users.find().pretty()
{
        "_id" : ObjectId("53bfc9f54ed48cddc53effa8"),
        "name" : "Satish",
        "email" : {
                "work" : "[email protected]",
                "personal" : "[email protected]"
        }
}

Here we have a sub-document called “email” – with work and personal keys.

Dot Notation To Access Sub Object: MongoDB


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

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



Finding document using sub-object

1
2
3
4
5
6
7
> db.users.find({"email": {"work": "[email protected]", 
                                   "personal": "[email protected]"}});
{ "_id" : ObjectId("53bfc9f54ed48cddc53effa8"), 
  "name" : "Satish", 
  "email" : { "work" : "[email protected]", 
              "personal" : "[email protected]" } 
}

But if the order of fields is changed it doesn’t retrieve the document, as it does not match the BSON bytes stored in the database.

Finding document using sub-object: Does not work

1
2
3
> db.users.find({"email": {"work": "[email protected]"}});
> db.users.find({"email": {"personal": "[email protected]", 
                           "work": "[email protected]"}});

Finding document with dot notation

1
2
3
4
5
> db.users.find({"email.work": "[email protected]"});
{ "_id" : ObjectId("53bfc9f54ed48cddc53effa8"), 
  "name" : "Satish", 
  "email" : { "work" : "[email protected]", 
              "personal" : "[email protected]" } }

Using dot notation is the best way to access sub-document or sub-object in MongoDB.

$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.

$exists, $type, $regex operators: MongoDB

In today’s video tutorial, lets learn to use $exists, $type and $regex operators.

mongodb-exisits-type-regex

Documents in our collection
test database, names collection.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
> use test
switched to db test
> show collections
names
system.indexes
 
> 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 }
 
> db.names.insert({"name": "Satish", "age": 27});
WriteResult({ "nInserted" : 1 })
 
 
> 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
}

Observe the documents – it has some names which are in alphabetical order. Last document has an extra field called age. And another odd entry is a document with name as 25.

$exists take 2 values, true or false

1
2
3
4
5
6
7
8
9
10
11
12
13
> db.names.find({"age": {$exists: true}});
{ "_id" : ObjectId("53beaa0f6a8a31dc255d4589"), "name" : "Satish", "age" : 27 }
 
> db.names.find({"age": {$exists: false}});
{ "_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 }

If $exists is true, it retrieves documents which has the specified field. If $exists is false, then it retrieves all the documents which do not have the specified field.

$exists, $type, $regex operators: MongoDB


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

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



$type take numeric value(BSON specification)

1
2
3
4
5
6
7
8
9
10
11
12
13
> db.names.find({"name": {$type: 1}});
{ "_id" : ObjectId("53be5e3704cc1cb0a7bfc3c8"), "name" : 25 }
 
> db.names.find({"name": {$type: 2}});
{ "_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("53beaa0f6a8a31dc255d4589"), "name" : "Satish", "age" : 27 }
TypeNumber
Double1
String2
Object3
Array4
Binary data5
Undefined6
Object id7
Boolean8
Date9
Null10
Regular Expression11
JavaScript13
Symbol14
JavaScript (with scope)15
32-bit integer16
Timestamp17
64-bit integer18
Min key255
Max key127

$regex (similar to perl regular expression)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
> db.names.find({"name": {$regex: "e"}});
{ "_id" : ObjectId("53be5d5204cc1cb0a7bfc3c1"), "name" : "Bebo" }
{ "_id" : ObjectId("53be5d5904cc1cb0a7bfc3c2"), "name" : "Chameli" }
{ "_id" : ObjectId("53be5d6104cc1cb0a7bfc3c3"), "name" : "Dev D" }
 
> db.names.find({"name": {$regex: "^E"}});
{ "_id" : ObjectId("53be5d6804cc1cb0a7bfc3c4"), "name" : "Emli" }
 
> db.names.find({"name": {$regex: "h$"}});
{ "_id" : ObjectId("53beaa0f6a8a31dc255d4589"), "name" : "Satish", "age" : 27 }
 
> db.names.find({"name": {$regex: "^[A-Z]"}});
{ "_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("53beaa0f6a8a31dc255d4589"), "name" : "Satish", "age" : 27 }
 
> db.names.find({"name": {$regex: "^[A-E]"}});
{ "_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" }

$regex: “e” retrieves all the documents in which small letter “e” is present.
$regex: “^E” retrieves all the documents which has it’s name beginning letter as “E”.
$regex: “h$” retrieves all the documents which has it’s name ending letter as “h”.
$regex: “^[A-Z]” retrieves all the documents which has it’s name starting with characters “A” to “Z”.
$regex: “^[A-E]” retrieves all the documents which has it’s name starting with characters “A” to “E”.

Note: There is more to regular expression, this video is just an introduction. We’ll cover more about regular expressions in a separate video of it’s own.

String Comparison: MongoDB

Today we shall see how we can compare strings using comparison operators like,
$ne – not equal to
$gt – greater than
$gte – greater than or equal to
$lt – less than
$lte – less than or equal to

(In our video we have clearly illustrated the use of $lt and $gt. Your task is to understand it and try other comparison operators, and you can share your results in the comment section below.)

red apple green apple Comparison Operators: MongoDB

Related Read: Comparison Operators: MongoDB

Documents in our collection
test database, names collection.

1
2
3
4
5
6
7
8
9
10
11
 
> 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 }

Here we have names in alphabetical order, and an odd name with numeric value 25.

We’ll query the collection using $lt operator.

1
2
3
4
 
> db.names.find({"name": {$lt: "C"}});
{ "_id" : ObjectId("53be5d4604cc1cb0a7bfc3c0"), "name" : "Alia" }
{ "_id" : ObjectId("53be5d5204cc1cb0a7bfc3c1"), "name" : "Bebo" }

It prints all strings which are lexicographically less than the capital letter “C”.

We’ll query the collection using $lt and $gt operator.

1
2
3
4
5
 
> db.names.find({"name": {$lt: "F", $gt: "C"}});
{ "_id" : ObjectId("53be5d5904cc1cb0a7bfc3c2"), "name" : "Chameli" }
{ "_id" : ObjectId("53be5d6104cc1cb0a7bfc3c3"), "name" : "Dev D" }
{ "_id" : ObjectId("53be5d6804cc1cb0a7bfc3c4"), "name" : "Emli" }

This outputs strings which are greater than character “C” and less than character “F”.

Here we check for names/strings which are less than character “C” as well as greater than character “C”.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
> db.names.find({"name": {$lt: "C"}});
{ "_id" : ObjectId("53be5d4604cc1cb0a7bfc3c0"), "name" : "Alia" }
{ "_id" : ObjectId("53be5d5204cc1cb0a7bfc3c1"), "name" : "Bebo" }
 
 
> db.names.find({"name": {$gt: "C"}});
{ "_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" }
>

But in both cases the entry with name as 25 doesn’t appear. This concludes that, in MongoDB while we compare string/character using comparison operator, the comparison occurs only between strings and character and not with other datatypes.

Comparison Operators on Strings: MongoDB


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

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



Note:
Lexicographical order: In mathematics, the lexicographic or lexicographical order is a generalization of the way the alphabetical order of words is based on the alphabetical order of their component letters.