Thursday, 28 November 2013

MongoDB Create and drop methods

Create Database:

If you want to create a database with name <mydb>, then use DATABASE statement would be as follows:

>use mydb
switched to db mydb


To check your currently selected database use the command db

>db
mydb


If you want to check your databases list, then use the command show dbs.

>show dbs
local     0.78125GB
mydb  0.23012GB
test      0.23012GB

Drop Database:

db.dropDatabase()

Create Collection:

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 used to specify configuration of collection

ParameterTypeDescription
NameStringName of the collection to be created
OptionsDocument(Optional) Specify options about memory size and indexing

Options parameter is optional, so you need to specify only name of the collection. Following is the list of options you can use:


FieldTypeDescription
cappedBoolean(Optional) If true, enables a capped collection. Capped collection is a collection fixed size collecction that automatically overwrites its oldest entries when it reaches its maximum size. If you specify true, you need to specify size parameter also.
autoIndexIDBoolean(Optional) If true, automatically create index on _id field.s Default value is false.
sizenumber(Optional) Specifies a maximum size in bytes for a capped collection. If If capped is true, then you need to specify this field also.
maxnumber(Optional) Specifies the maximum number of documents allowed in the capped collection.

While inserting the document, MongoDB first checks size field of capped collection, then it checks max field.

EXAMPLES:

Basic syntax of createCollection() method without options is as follows

>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


Following example shows the syntax of createCollection() method with few important options:

>db.createCollection("mycol", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } )
{ "ok" : 1 }
>


In mongodb you don't need to create collection. MongoDB creates collection automatically, when you insert some document.

>db.tutorialspoint.insert({"name" : "tutorialspoint"})
>show collections
mycol
mycollection
system.indexes
tutorialspoint
>


Drop Collection:

Basic syntax of drop() command is as follows

db.COLLECTION_NAME.drop()

Example:

>db.mycollection.drop()
true
>

MongoDB Environment

To start a mongodb, following are the commands:

Command used to start the mongod.exe is running for connections,

C:\Mongo\mongodb-win32-x86_64-2008plus-2.4.8\bin>mongod.exe --dbpath "c:\Mongo\data"

Command to run the mongodb,

C:\Mongo\mongodb-win32-x86_64-2008plus-2.4.8\bin>mongo.exe

MongoDB Introduction



  •  MongoDB is an open-source document database, and leading NoSQL database. MongoDB is written in      c++.
  • MongoDB is a cross-platform, document oriented database that provides, high performance, high  availability, and easy scalability. MongoDB works on concept of collection and document.
  • Below given table shows the relationship of RDBMS terminology with MongoDB



RDBMSMongoDB
DatabaseDatabase
TableCollection
Tuple/RowDocument
columnField
Table JoinEmbedded Documents
Primary KeyPrimary Key (Default key _id provided by mongodb itself)
Database Server and Client
Mysqld/Oraclemongod
mysql/sqlplusmongo


  • Any relational database has a typical schema design that shows number of tables and the relationship between these tables. While in MongoDB there is no concept of relationship


Advantages of MongoDB over RDBMS


  • Schema less : MongoDB is document database in which one collection holds different different documents. Number of fields, content and size of the document can be differ from one document to another.
  • Structure of a single object is clear
  • No complex joins
  • Deep query-ability. MongoDB supports dynamic queries on documents using a document-based query language that's nearly as powerful as SQL
  • Tuning
  • Ease of scale-out: MongoDB is easy to scale
  • Conversion / mapping of application objects to database objects not needed
  • Uses internal memory for storing the (windowed) working set, enabling faster access of data


Why should use MongoDB


  • Document Oriented Storage : Data is stored in the form of JSON style documents
  • Index on any attribute
  • Replication & High Availability
  • Auto-Sharding
  • Rich Queries
  • Fast In-Place Updates
  • Professional Support By MongoDB


Where should use MongoDB?


  • Big Data
  • Content Management and Delivery
  • Mobile and Social Infrastructure
  • User Data Management
  • Data Hub



DBT - Models

Models are where your developers spend most of their time within a dbt environment. Models are primarily written as a select statement and ...