Skip to main content

The "case" does matter in MongoDb too!

Sometimes, when working on a particular Technology, we pay a very close attention to some of the features or ways to work on in that technology. However, we tend to ignore other ways. We take a few things for granted, till the day we come across something to the contrary.

This is what happened while working with collections and documents in MongoDb.

I have this Electronics collection :


> db.Electronics.find()
{ "_id" : ObjectId("55685b94fd45377c7f18bccc"), "category" : "TV", "Sales_Date" : "17-11-2014", "amount" : "32000", "currency" : "INR", "Brand" : "Son
y" }
{ "_id" : "00005", "category" : "Laptop", "Sales_Date" : "05-10-2014", "amount" : "28000", "currency" : "INR", "Brand" : "Acer" }

Now I wanted to add a TV of Samsung brand which costs Rs. 27500 and with sales date as 10 June 2016.

I tried to add it as follows:
> db.electronics.insert ( {_id :0006 , category :"TV", brand :"Samsung OLED", sales_date : "10-06-2016", amount :"27500", currency :"INR" })

Now I queried on the collection Electronics again:

> db.Electronics.find()
{ "_id" : ObjectId("55685b94fd45377c7f18bccc"), "category" : "TV", "Sales_Date" : "17-11-2014", "amount" : "32000", "currency" : "INR", "Brand" : "Son
y" }
{ "_id" : "00005", "category" : "Laptop", "Sales_Date" : "05-10-2014", "amount" : "28000", "currency" : "INR", "Brand" : "Acer" }


Hey, where is the Samsung OLED TV?

Any idea, what happened?

Well, for Mongodb, the collection "electronics" is different from "Electronics".
So, when I added the document by using db.electronics.insert...
MongoDb created a new collection called electronics as can be seen from these two statements:

> show collections
Appliances
Electronics
collection
electronics
system.indexes

The above shows two different collections - Electronics and electronics
Also,
db.electronics.find()
shows:
> db.electronics.find()
{ "_id" : 6, "category" : "TV", "brand" : "Samsung OLED", "sales_date" : "10-06-2016", "amount" : "27500", "currency" : "INR" }
>
>
--

The right way to add would be :

db.Electronics.insert ( {_id :0007 , category :"TV", brand :"Samsung OLED", sales_date : "10-06-2016", amount :"27500", currency :"INR" })

I also add two more entries:

db.Electronics.insert ( {_id :0008 , category :"TV", brand :"Panasonic LED", sales_date : "14-06-2016", amount :"31500", currency :"INR" })
db.Electronics.insert ( {_id :0009 , category :"TV", brand :" OLED", sales_date : "12-06-2016", amount :"15500", currency :"INR" })

And final result is now as expected:


> db.Electronics.find()
{ "_id" : ObjectId("55685b94fd45377c7f18bccc"), "category" : "TV", "Sales_Date" : "17-11-2014", "amount" : "32000", "currency" : "INR", "Brand" : "Son
y" }
{ "_id" : "00005", "category" : "Laptop", "Sales_Date" : "05-10-2014", "amount" : "28000", "currency" : "INR", "Brand" : "Acer" }
{ "_id" : 7, "category" : "TV", "brand" : "Samsung OLED", "sales_date" : "10-06-2016", "amount" : "27500", "currency" : "INR" }
{ "_id" : 8, "category" : "TV", "brand" : "Panasonic LED", "sales_date" : "14-06-2016", "amount" : "31500", "currency" : "INR" }
{ "_id" : 9, "category" : "TV", "brand" : " OLED", "sales_date" : "12-06-2016", "amount" : "15500", "currency" : "INR" }
>

Comments

Popular posts from this blog

India's exciting one wicket win over Australia in Mohali test

In an exciting finish in the first test match at Mohali, India beat Australia by 1 wicket. India had to make 216 to win, and at 1 stage India were 124 for 8. Then V.V.S.Laxman and Ishant Sharma had a superb partnership of 81 runs and took India to victory. Laxman remained unbeated on 73, and Ishant made 31. The place where I am currently staying does not subscribes to the sports channel showing this match, but I would surely love to watchthe highlights of this match.

Yummy Cooking Pumpkin Dessert // Pumpkin Dessert Recipe // Yummy Cooking...