How To Get Distinct Values In MongoDB(NoSql Database)

Praveen Singh         No comments

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...

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.

Published by Praveen Singh

A blogger by passion.You can find me tucked in my bed and blogging on weekends when not roaming around. Besides blogging, I love music and you can find my songs on my fb page:PraveenUnplugged
Follow on Youtube : Videos On Latest Happenings |ThingsToKnow
Follow us Talend In Action

0 responses:

© 2015 Techie's House. Designed by Bloggertheme9