Module - 6
Module - 6
Database
View Database:
To see how many databases are present in your MongoDB server, write the
following statement in the mongo shell:
show dbs
For Example:
Here, we freshly started MongoDB so we do not have a database except
these three default databases, i.e, admin, config, and local.
Creating Database:
In the mongo shell, you can create a database with the help of the
following command:
use database_name
This command actually switches you to the new database if the given name
does not exist and if the given name exists, then it will switch you to the
existing database. Now at this stage, if you use the show command to see
the database list where you will find that your new database is not present
in that database list because, in MongoDB, the database is actually created
when you start entering data in that database.
For Example:
Here, we create a new database named GeeksforGeeks using the use
command. After creating a database when we check the database list we
do not find our database on that list because we do not enter any data in
the GeeksforGeeks database.
Collection
Collections are just like tables in relational databases, they also store data,
but in the form of documents. A single database is allowed to store multiple
collections.
Schemaless:
Before moving further first you should learn about the naming restrictions
for fields:
• The field names are of strings.
• The _id field name is reserved to use as a primary key. And the value
of this field must be unique, immutable, and can be of any type other
than an array.
• The field name cannot contain null characters.
• The top-level field names should not start with a dollar sign ($).
Document Size: The maximum size of the BSON document is 16MB. It
ensures that the single document does not use too much amount of RAM or
bandwidth(during transmission). If a document contains more data than
the specified size, then MongoDB provides a GridFS API to store such type
of documents.
Important Notes –
• A single document may contain duplicate fields.
• MongoDB always saves the order of the fields in the documents
except for the _id field (which always comes in the first place) and
the renaming of fields may change the order of the fields in the
documents.
• _id Field: In MongoDB, every document store in the collection must
contain a unique _id field it is just like a primary key in a relational
database. The value of the _id field can be set by the user or by the
system (if the user does not create an _id field, then the system will
automatically generate an ObjectId for _id field).
o When you create a collection MongoDB automatically creates a
unique index on the _id field.
o The _id field is the first field of every document.
o The value of the _id field can be of any BSON type except
arrays.
o The default value of the _id field is ObjectId.
Example #1:
Here, name, branch, course, and paid field contain values of string type.
amount field contains the value of integer type and _id field is generated by
the system.
Example #2:
Method Description
Example 1: In this example, we are inserting details of a single student in the form of
document in the student collection using db.collection.insertOne() method.
Example 2: In this example, we are inserting details of the multiple students in the form of
documents in the student collection using db.collection.insertMany() method.
Read Operations –
The Read operations are used to retrieve documents from the collection, or in other words,
read operations are used to query a collection for a document. You can perform read
operation using the following method provided by the MongoDB:
Method Description
.pretty() : this method is used to decorate the result such that it is easy to read.
Example : In this example, we are retrieving the details of students from the student collection
using db.collection.find() method.
Update Operations –
The update operations are used to update or modify the existing document in the collection.
You can perform update operations using the following methods provided by the MongoDB:
Method Description
Method Description
Example 1: In this example, we are deleting a document from the student collection using
db.collection.deleteOne() method.
Example 2: In this example, we are deleting all the documents from the student collection
using db.collection.deleteMany() method.
Redis - Data Types
Strings
Redis string is a sequence of bytes. Strings in Redis are binary safe,
meaning they have a known length not determined by any special
terminating characters. Thus, you can store anything up to 512
megabytes in one string.
Example
redis 127.0.0.1:6379> SET name "tutorialspoint"
OK
redis 127.0.0.1:6379> GET name
"tutorialspoint"
In the above example, SET and GET are Redis commands, name is
the key used in Redis and tutorialspoint is the string value that is
stored in Redis.
Hashes
A Redis hash is a collection of key value pairs. Redis Hashes are
maps between string fields and string values. Hence, they are used
to represent objects.
Example
redis 127.0.0.1:6379> HMSET user:1 username tutorialspoint
password
tutorialspoint points 200
OK
redis 127.0.0.1:6379> HGETALL user:1
1) "username"
2) "tutorialspoint"
3) "password"
4) "tutorialspoint"
5) "points"
6) "200"
In the above example, hash data type is used to store the user's
object which contains basic information of the user. Here HMSET,
HGETALL are commands for Redis, while user:1 is the key.
Lists:
Redis lists are linked lists of string values. Redis lists are frequently
used to:
Basic commands
• LPUSH adds a new element to the head of a list; RPUSH adds to the
tail.
• LPOP removes and returns an element from the head of a
list; RPOP does the same but from the tails of a list.
• LLEN returns the length of a list.
• LMOVE atomically moves elements from one list to another.
Examples
(integer) 2
"bike:1"
"bike:2"
Basic commands
• SADD adds a new member to a set.
• SINTER returns the set of members that two or more sets have in common
(i.e., the intersection).
• SCARD returns the size (a.k.a. cardinality) of a set.
Examples
Store the sets of bikes racing in France and the USA. Note that if
you add a member that already exists, it will be ignored.
1)
)
3 Which bikes are common in both races?
Let's start with a simple example, we'll add all our racers and the
score they got in the first race:
Note that the ZRANGE order is low to high, while the ZREVRANGE order is high
to low: