Tuesday, 24 June 2014

MONGO DB BASICS FOR BEGINNERS


Mapping RDBMS TO 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

Basic Queries:

Use database:
>use [database_name] #create and switch to that database
Ex: >use mongo
#switch to mongo database

show collections:
>show collections #To show collections in mongo database.

Current Database:
>db #To show current database

To show Databases:
>show dbs #To show all databases
Note: Its not displaying sari database because there is no data in sari database. first insert some records in database.

Drop Database:
>db.dropdatabase()#To drop a current database

Select Records:
>db.collectionName.findOne()
#To show first record in a collection
>db.collectionName.find().pretty
#To show all fields in a format
>db.collectionName.find().limit(5)
>db.collectionName.find( { _id: 5 } )
#displays records with id=5
>db.collection.find( { field: { $gt: value1, $lt: value2 } } );
Ex:>db.students.find( { score: { $gt: 0, $lt: 2 } } )
#To specify ranges
Create a Collection:
>db.createCollection(nameoptions)
#To create a collection
Ex: db.createCollection(student, {name: <string>, qual: <string>, age: <number>})

Insert Values Into Collection:

>db.collectionname.insert( { field1: value1, field2: val2} )
Ex:db.students.insert( { name: "ss", qual: "btech", age: 15 } )

To Remove Specific Documents:
>db.collection.remove()
#to delete a document in collection
Ex:>db.students.remove( { age: { $gt: 20 } } )






No comments:

Post a Comment