MongoDB can be used as a file system, taking advantage of load balancing and data replication features over multiple machines for storing files.
Introduction
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:
Objective
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 documents.so 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
db.foo.save ({_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
db.foo.find ()
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
db.foo.save
({_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
structure.as shown below…
db.foo.save({_id: 1})
db.foo.save({_id 3.14})
db.foo.save({_id "hello"})
db.foo.save({_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.
Conclusion
In this
article we have seen how to save data in Mongo DB and various options to save.
Reference
http://bsonspec.org/http://www.mongodb.org/