First, we will see how to delete a document (row) from the collection (table) and then how to drop whole collection (table).

Let's start...

SQL's way...

Suppose we have a info table with Name column....

Now, if I have to delete a row from info table, I would do something like this :

Delete from info where Name = 'Alex'

Dropping table...

Drop table info

OR

Truncate table info

NOSQL's way....

db.info.remove({ Name : 'Alex' })

Dropping Collection....

db.info.drop()













Today, We will try to discover how to get distinct values from the MongoDB database.

Suppose we have table info in the database...

In sql database, we would do something like this to create info table:

create table info
(
      Name  Varchar(30),
      Place   Varchar(30)
)

Once this is done, we would insert some duplicate values in the table :

Insert into info (Name , Place ) values ( "Praveen", "Pune")
Insert into info (Name , Place ) values ( "Praveen", "Lucknow")
Insert into info (Name , Place ) values ( "Alex", "NY")
Insert into info (Name , Place ) values ( "Alex", "Manhattan")

So, now we have some duplicate values in the database.

To get the distinct values :

select distinct Name from info

Now, MongoDB's way.....

create table and insert the values in just one step .. ( Isn't it simple??)


select the data from the table...

db.info.find()


Now to get the distinct values :


Let's see what is this ...

db.collection.distinct : Finds the distinct values for a specified field across a single collection and returns the results in an array.

First, visit here and download the MongoDB setup based on your system compatibility, like my windows is 32 bit so I had installed 32 bit version of MongoDB.



The setup file (mongodb-win32-i386-2.4.9.zip) will be around 88 mb and once it is done , just extract this archived file. If you go to the bin folder , you will be able to see the files as mentioned below :


We have to deal with two files here :
  1. Mongo     (the database shell)
  2. Mongod   (the core database server)

Also, Before you can start the MongoDB service, you need to create a data directory for MongoDB to store its files in.By default, MongoDB stores the data in the /data/db directory on Unix-based systems (e.g.,
Linux and OS X) and in the C:\data\db directory on Windows.

Open your command prompt and go to the bin directory , write mongod and hit enter :


Now open the second instance of the command prompt , go to the bin directory and write mongo :


Done !!
© 2015 Techie's House. Designed by Bloggertheme9