Les10 MongoDB part 1 intro with NOSQL (1)
Les10 MongoDB part 1 intro with NOSQL (1)
docx
With NOSQL
Was Les09 in week 10 renamed to match week
Topics
1 NoSQL Overview
(Not only SQL became shortened to NoSQL)
2 MongoDB
An introduction for beginners covering MongoDB for the next 3 weeks
Each unit of data is called a COLUMN and it has a defined type, size and
constraints
The combined columns are called a ROW
NoSQL databases
- are not primarily built on tables and as a result do not use SQL for data
manipulation
NoSQL is not a database but a term to refer to databases that attempt to solve
problems of scalability and availability against atomicity and consistency
Although these are excellent qualities, they are incompatible with availability and performance on
applications of web scale.
Example :
if a company like Amazon were to use a system like this imagine how slow it would be if I proceed to buy a
book and a transaction is being done it will lock a part of the database, specifically locking the inventory,
and every other person in the world will have to wait until I complete my transaction. Obviously, this doesn't
work well
Leads to BASE
soft state the state of the system may change over time at times
without any input. It will eventually become consistent
Development time
It is reported that there would be less development time because one does not
have to deal with complex SQL queries. If you remember you needed to join
tables to get data that was stored across multiple tables in order to create your
final views for the user.
Speed
Even with small amounts of data that you have, if you can deliver it in
milliseconds rather than hundreds of a millisecond especially when using mobile
devices, you have a high probability of convincing users of your system that it's
a good idea
Indexing
Aggregations
File Storage
Database
Database is a physical container for collections.
Each database gets its own set of files on the file system.
A single MongoDB server typically has multiple databases.
Collections
Collections contain sets or groups of documents and function which is the
equivalent of relational database tables.
Documents
Documents consist of key-value pairs which are the basic unit of data in
MongoDB.
How to scale
1 Large machines can be used to scale up
- Expensive
- There may be a limit on physical machines
2 Partitioning
- storage space achieved by adding servers and computers to your clusters
- Cheaper
- Added difficulty managing 1000s of machines
Vertical scaling
Vertical scaling can essentially resize your server with no change to your code. It is the ability to increase
the capacity of existing hardware or software by adding resources. Vertical scaling is limited by the fact that
you can only get as big as the size of the server.
apartment building that has many rooms and floors where people move in and out all the time. In this
apartment building, 200 spaces are available but not all are taken at one time. So, in a sense, the
apartment scales vertically as more people come and there are rooms to accommodate them.
Note: If the 200-space capacity is not exceeded, life is good.
Restaurant - capacity
Horizontal scalability
Means increasing capacity by connecting multiple hardware or software entities so that they work as a
single logical unit. When servers are clustered , the original server is being scaled out horizontally.
COLLECTION
- similar idea to a table, but not a fixed schema
MongoDB creates a database, if it does not already exist, when you insert the
first document into your database
use EmployeeDB
local
This Database stores any local connections on a single server
config
a config server stores the clusters metadata great
SEE databases
show dbs
>show dbs
local 0.78125GB
test 0.23012GB Notice that MYDB is not there
To display the database, you need to insert at least one document into it.
db.mydb.insert ({"name":"first document"})AN INSERT COMMAND
another example
show dbs
local 0.78125GB
mydb 0.23012GB NOW IT WILL SHOW
test 0.23012GB
>db.dropDatabase()
>{ "dropped" : "mydb", "ok" : 1 }
Proof
>show dbs
local 0.78125GB Will be different depending on
what was there from before
test 0.23012GB
>
>db.createCollection("mycollection")
{ "ok" : 1 }
SEE COLLECTIONS
>show collections
mycollection here it is
And is
- Case sensitive
db.myEmployeeDB.insert (
{
"Employeeid" : 1,
"EmployeeName" : "Martin"
}
)
OUTPUT
WriteResult({ "nInserted" : 1 })
show collections
mycollections
myEmployeesDB
show documents
db.myEmployeeDB.find()
db.dropDatabase() removes the current database and all data inside the
database.
Go to that database
Use EmployeeDB
> db.dropDatabase()
true
>
Example:
Example:
Key: "greeting"
Value: "Hello, world"
Aside:
_id is 12 bytes hexadecimal number unique for every document in a collection. 12 bytes are divided as
follows −
_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes
incrementer)
Example:
{"greeting" : "Hello, world!", "greeting" : "Hello, MongoDB!“}
Step 2) Within the "insert" command, add the required Field Name and Field Value for the document
which needs to be created.
Code Explanation:
1. The first part of the command is the "insert statement" which is the statement used to insert
a document into the collection.
2. The second part of the statement is to add the Field name and the Field value, in other words,
what is the document in the collection going to contain.
If the command is executed successfully, the following Output will be shown
Output:
The output shows that the operation performed was an insert operation and that one record was
inserted into the collection.
> db.createCollection("empDetails")
{ "ok" : 1 }
InsertOne() method
db.empDetails.insertOne(
{
First_Name: "Radhika",
Last_Name: "Sharma",
Date_Of_Birth: "1995-09-26",
e_mail: "[email protected]",
phone: "9848022338"
})
OUTPUT:
{
"acknowledged" : true,
"insertedId" : ObjectId("5dd62b4070fb13eec3963bea")
}
insertOne
db.empDetails.inse {
rtMany( "acknowledged" : true,
[ "insertedIds" : [
{ ObjectId("5dd631f270fb13eec3963bed"),
ObjectId("5dd631f270fb13eec3963bee"),
First_Name: ObjectId("5dd631f270fb13eec3963bef")
"Radhika", ]
}
Last_Name:
"Sharma",
db.empDetails.drop()
Date_Of_Birth:
"1995-09-26", Remove a document
e_mail:
"radhika_sharma.12
[email protected]",
The remove function deletes documents.
phone:
"9000012345"
}, Reinsert the many data above
{
Do the find db.empDetails.find()
First_Name: { "_id" : ObjectId("5fb29639d1db3d91a34ac04e"), "First_Name" : "Radhika", "Last_Name" :
"Rachel", "Sharma", "Date_Of_Birth" : "1995-09-26", "e_mail" : "[email protected]",
"phone" : "9000012345" }
Last_Name:
"Christopher", { "_id" : ObjectId("5fb29639d1db3d91a34ac04f"), "First_Name" : "Rachel", "Last_Name" :
"Christopher", "Date_Of_Birth" : "1990-02-16", "e_mail" :
"[email protected]", "phone" : "9000054321" }
Date_Of_Birth: { "_id" : ObjectId("5fb29639d1db3d91a34ac050"), "First_Name" : "Fathima", "Last_Name" :
"1990-02-16", "Sheik", "Date_Of_Birth" : "1990-02-16", "e_mail" : "[email protected]",
"phone" : "9000054321" }
e_mail:
"Rachel_Christophe
[email protected]",
824457402.docx by rt -- 6 December 2024 27 of 30
Following example will remove all the documents whose First_name is …
db.empDetails.remove ({"First_Name" : "Radhika"})
How to check it
db.empDetails.find()
{ "_id" : ObjectId("5fb29639d1db3d91a34ac04f"), "First_Name" : "Rachel", "Last_Name" : "Christopher", "Date_Of_Birth" : "1990-
02-16", "e_mail" : "[email protected]", "phone" : "9000054321" }
{ "_id" : ObjectId("5fb29639d1db3d91a34ac050"), "First_Name" : "Fathima", "Last_Name" : "Sheik", "Date_Of_Birth" : "1990-02-
16", "e_mail" : "[email protected]", "phone" : "9000054321" }
Just an extra to make it nicer to read – do not believe this is on the course but showing it anyway
db.empDetails.find().pretty()
{
"_id" : ObjectId("5fb29639d1db3d91a34ac04f"),
"First_Name" : "Rachel",
"Last_Name" : "Christopher",
"Date_Of_Birth" : "1990-02-16", A bit better layout.
"e_mail" : "[email protected]",
"phone" : "9000054321"
}
{
"_id" : ObjectId("5fb29639d1db3d91a34ac050"),
"First_Name" : "Fathima",
"Last_Name" : "Sheik",
"Date_Of_Birth" : "1990-02-16",
"e_mail" : "[email protected]",
"phone" : "9000054321"
}
db.empDetails.remove({})
NOTE: removes the documents
WriteResult({ "nRemoved" : 2 })
Proof
db.empDetails.find()