Mapping RDBMS TO MongoDB:
RDBMS | MongoDB |
---|---|
Database | Database |
Table | Collection |
Tuple/Row | Document |
column | Field |
Table Join | Embedded Documents |
Primary Key | Primary Key (Default key _id provided by mongodb itself) |
Database Server and Client | |
Mysqld/Oracle | mongod |
mysql/sqlplus | mongo |
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 databasesEx: >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:
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(name, options)
#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