Introduction to Mongo db Storing Data Part2

Rama Sagar
Posted by in NoSql category on for Beginner level | Points: 250 | Views : 4744 red flag

MongoDB can be used as a file system, taking advantage of load balancing and data replication features over multiple machines for storing files.
Read Introduction to Mongo db Storing Data before this article.


In this article we will learn how to save data in Mongo dB with a practical example

Previous articles have provided an introduction to Mongo dB and the setup and installation, and few command line options.You can get them from the following:


The objective of this article is to learn about saving data in Mongo database.

Saving Data

 Let’s start from scratch….

We are in some default database where dB is our current database…Lets us first check if there is any data in there we can do it by issuing a command as shown below…

The image indicates there are no collections. A collection in mongo defines the scope of interaction with the we can issue commands against a specific collection to store and retrieve data. Because it’s not a relational database we cannot issue commands across several collections…

Ok now let’s save our first record ({_id:1, x: 10})

Here dB means the database name where we are operating.

Foo is the name of the collection we are going to save this document into

Save indicates the saving of record.

We can find the document we just saved by issuing the command ()

Now if we check the collections we will have two collections one is foo and other is system. Indexes.

Every document must have an id that’s because in order to have fast access we want an index in an Id field.

Let’s insert same record into student collection ({_id:1, x: 10})

Now if we see the collections we can have the following result

Let’s check the indexes by issuing command db.system.indexes.find ()

We can see mongo has created an index on the Id field the key is id on the test foo and test student collections inside the database…

Let us see what data field the id supports..

Mongo can have an id that is numeric, an integer, a floating point or a UTC date time shown below…{_id: 1}){_id 3.14}){_id "hello"}){_id ISODate()})

The only data type which is excluded is an array if we try to use an array it throws an exception as shown below.


In this article we have seen how to save data in Mongo DB and various options to save.


Page copy protected against web site content infringement by Copyscape

About the Author

Rama Sagar
Full Name: RamaSagar Pulidindi
Member Level: Silver
Member Status: Member,MVP
Member Since: 12/30/2012 1:51:40 AM
Country: India
A Software Profesional working in Microsoft .NET technologies since year 2008, and I work for Dake ACE. I am passionate about .NET technology and love to contribute to the .NET community at Dot Net Funda

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)