Showing posts with label MongoDB Database. Show all posts
Showing posts with label MongoDB Database. Show all posts

Wednesday, January 29, 2014

Logical Operators in MongoDB.

In this post we will explore the Logical operators of MongoDB. Just like RDBMS such as Oracle, SQL Server, DB2 have logical operators, MongoDB has its own set of Comparison operators.

RDBMS Logical Operators MongoDB Operator
AND {,}
OR $or:[ ... ]

Let us see example of MongoDB operators.

AND operator

In MongoDB the key/vaue pair passed into a curley braces ({...}) and separated by comma (,) act as AND logical operator. For example to read document (Row) from deptdetails (table) collection using AND operator we can write following command.

db.deptdetails.find({DeptCode:30,DeptName:"Accounts"}).pretty()

Here we are reading data where DeptName is Accounts AND DeptCode is 30.

OR operator

MongoDB has provided $or for OR operator. They key/value pair inside $or[ … ] is use to pass separate expression values. For example to read data from deptdeails collection (table) where DeptName is HR or DeptCode is 30 we can write the statement like this

db.deptdetails.find(
{
$or:
[
{DeptCode:30},
{DeptName:"HR"}
]
}
).pretty()

What is MongoDB?
How to get Started with MongoDB
How to create or drop Database in MongoDB?
How to create Collection (Table) in MongoDB?
... More

Tuesday, January 28, 2014

How to create Collection (Table) in MongoDB?

Today, I will share how we can create collection (table) in MonogoDB. Just like in Oracle, SQL Server or any other RDBMS, tables are created before storing data into them; we create Collection in MongoDB to store data.

In RDBMS you issue CREATE TABLE command along with Column names and their data types along with other constraints (Primary key, constraints etc.).

In MongoDB you define collection. You do not need to define column or their data type. In MongoDB each of the rows that you store can have different fields or columns. Unlike with RDBMS MongoDB gives you flexibility of dynamic columns. So for 1 row you may have 5 fields but for second row you can store 10 fields.

In MongoDB you can create collection by two methods. First you can use db.createCollection() method.

You can pass the collection name as a parameter.

db.createCollection(collection_name, options)

In below example, I have created table employeeList by issuing the db.createCollection(“employeeList”) command.

The second method is you can use the db.Collection_Name.Insert() command. The insert() method add a document in the collection. If Collection does not exist then it will create the collection.

To add a single document (row) into a collection (table) we can issue following command.

db.Collection_Name.Insert({Key1:Value1,Key2:Value2, ... KeyN:ValueN})

db.employeeList.insert({EmpName:"Sam Pitroda",Age:25,DOJ:04/04/2011,Salary:2695,Gender:"M"})

To add multiple documents (row), we can issue commands like this

db.employeeList.insert(
    {EmpName:"Anil Shastri",Age:39,DOJ:11/19/2007,Salary:4911,Gender:"M"},
    {EmpName:"Bill Rama",Age:33,DOJ:9/10/2006,Salary:4280,Gender:"M"},
    {EmpName:"John Butler",Age:23,DOJ:12/28/2009,Salary:2612,Gender:"M"},
    {EmpName:"Srini Arya",Age:36,DOJ:12/08/2006,Salary:2316,Gender:"F"} )

To see the list of Collections (tables) in the current database, we can use following command

show collections

What is MongoDB?
How to get Started with MongoDB
How to create or drop Database in MongoDB?

Monday, January 27, 2014

How to create or drop Database in MongoDB?

Today, we will explore how we can create and drop databases in MongoDB. By default, 32-bit windows MongoDB comes with a two database named "local" and "Test".

To see the list of all databases in MongoDB we can use show dbs command.

show dbs

To create a new database in MongoDB we need to issue use statement.

use Database_Name

use Employee

This will create a new database with the name Employee.

If databases already exist than use command will let MongoDB to switch over to that database.

To drop or delete a database in MongoDB we will issue following syntax

use database_name
db.dropDatabase()

To drop Employee database we need to issue following command.

Use Employee
db.dropDatabase()

To see what the current database you are working with, we can issue following command

db

What is MongoDB?

Popular Posts

Real Time Web Analytics