Database-Technology Removed (2)
Database-Technology Removed (2)
The _id is a special field that is found on all documents in Mongo, just like
ROWID in Oracle. In MongoDB, _id can be assigned by the user, as long
as it is unique.
Every object within the MongoDB database contains this unique identifier
_id to distinguish that object from every other object. It is added
automatically to every document you create in a collection.
Figure: The MongoDB database model
Figure: A typical relational database model
What Is a Document Database?
{
"firstname": "Martin",
"likes": [ "Biking", "Photography" ],
"lastcity": "Boston"
}
The above document can be considered a row in
a traditional RDBMS.
Let’s look at another document
{
"firstname": "Pramod",
"citiesvisited": [ "Chicago", "London", "Pune", "Bangalore" ],
"addresses":
[
{ "state": "AK",
"city": "DILLINGHAM",
"type": "R"
},
{ "state": "MH",
"city": "PUNE",
"type": "R"
}
],
"lastcity": "Chicago"
}
• Looking at the documents, we can see that they are
similar, but have differences in attribute names.
This is allowed in document databases.
• The schema of the data can differ across
documents, but these documents can still belong to
the same collection—unlike an RDBMS where
every row in a table has to follow the same schema.
• We represent a list of citiesvisited as an array, or a
list of addresses as list of documents embedded
inside the main document.
• Embedding child documents as subobjects inside
documents provides for easy access and better
performance.
• If you look at the documents, you will see that some of
the attributes are similar, such as firstname or city.
At the same time, there are attributes in the second
document which do not exist in the first document,
such as addresses, while likes is in the first document
but not the second.
• MongoDB
• CouchDB
• Terrastore
• OrientDB
• RavenDB
• Lotus Notes
MongoDB Features
Database
• Database is a physical container for collections. A
single MongoDB server typically has multiple databases.
Collection
• Collection is a group of MongoDB documents. It is
the equivalent of an RDBMS table. A collection
exists within a single database. Collections do not
enforce a schema. Documents within a collection
can have different fields. Typically, all documents
in a collection are of similar or related purpose.
Document
• A document is a set of key-value pairs. Documents
have dynamic schema. Dynamic schema means that
documents in the same collection do not need to
have the same set of fields or structure, and
common fields in a collection's documents may hold
different types of data.
MongoDB Commands
• Command to Start MongoDB
sudo service mongodb start
• db Command
To check your currently selected database, use the command db
>db
It displays:
mydb
• show dbs Command
If you want to check your databases list, use the command show
dbs.
>show dbs
It displays:
local 0.78125GB
test 0.23012GB
Your created database (mydb) is not present in list. To display
database, you need to insert at least one document into it.
• insert command
>db.movie.insert({"name":"tutorials point"})
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
In MongoDB default database is test. If you didn't create any
database, then collections will be stored in test database.
MongoDB - Drop Database
dropDatabase() Method
MongoDB db.dropDatabase() command is used to drop a existing database.
Syntax
Basic syntax of dropDatabase() command is as follows −
>db.dropDatabase()
This will delete the selected database. If you have not selected any database, then
it will delete default 'test' database.
Example
First, check the list of available databases by using the command, show dbs.
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
>
If you want to delete new database <mydb>, then
dropDatabase() command would be as follows −
>use mydb
switched to db mydb
>db.dropDatabase()
>{ "dropped" : "mydb", "ok" : 1 }
>
Now check list of databases.
>show dbs
local 0.78125GB
test 0.23012GB
>
MongoDB - Create Collection
createCollection() Method
MongoDB db.createCollection(name, options) is used to create collection.
Syntax
Basic syntax of createCollection() command is as follows −
>db.createCollection(name, options)
In the command, name is name of collection to be created. Options is a
document and is used to specify configuration of collection.
>use test
switched to db test
>db.createCollection("mycollection")
{ "ok" : 1 }
>
You can check the created collection by using the command show collections.
>show collections
mycollection
system.indexes
The following example shows the syntax of createCollection() method
with few important options −
>db.tutorialspoint.insert({"name" : "tutorialspoint"})
>show collections
mycol
mycollection
system.indexes
tutorialspoint
>
MongoDB - Drop Collection
drop() Method
MongoDB's db.collection.drop() is used to drop a collection from the database.
Syntax
Basic syntax of drop() command is as follows −
db.COLLECTION_NAME.drop()
Example
First, check the available collections into your database mydb.
>use mydb
switched to db mydb
>show collections
mycol
mycollection
system.indexes
tutorialspoint
>
Now drop the collection with the name mycollection.
>db.mycollection.drop()
true
>
>show collections
mycol
system.indexes
tutorialspoint
>
DBCollection shopping =
database.getCollection("shopping");
shopping.setWriteConcern(REPLICAS_SAFE);
• WriteConcern can also be set per operation
by specifying it on the save command:
• WriteResult result = shopping.insert(order,
REPLICAS_SAFE);
• E-Commerce Applications
E-commerce applications often need to have flexible
schema for products and orders, as well as the
ability to evolve their data models without expensive
database refactoring or data migration
When Not to Use