Introduction To MongoDB
What is MongoDB?
MongoDB
is a NoSQL document-oriented database. MongoDB is written by using C++
language. NoSQL databases are much different than relational databases. In a real
database, you have to map out everything. You need to figure out the exact schema
what table you use and what field you use and all that stuff. But with NoSQL, there
is not like that. You should plan out the structure of your database and your
collections. But you don’t have to do any free structuring before you build
your application. An advantage is MongoDB is easy to scale. MongoDB uses documents
containing key-value pairs to store data. A collection is a group of MongoDB
documents. MongoDB uses collections instead of tables and uses documents instead
of rows and uses fields instead of columns.
MongoDB Syntax
MongoDB
uses use database_name syntax to
create a database
Create a collection
Collections
hold documents or records.
·
db.createCollection(name,
option) syntax
is used to create a collection
Student
is the name of the collection and option is a document that use to specify
configuration of the collection.
·
CreateCollection(name) syntax without option
Insert a document
·
db.faculty.insert() syntax is used to insert
documents. Faculty is the collection name of that command.
In The above example shows how a document is stored in a MongoDB collection.
Note:
You can add fields whatever you want to a document in a collection, even other
documents in that collection does not have that field.
Get a document
·
db.getCollection(“faculty”).find()
or db.faculty.find() syntax is used to
retrieve data from a collection called faculty.
Update a document (add a new
field to a document)
·
db.faculty.update({name:"Smith"},{$set:{gender:"male"}}) syntax
is used to add new field to the faculty document.
Remove
a document
·
db.faculty.remove({name:"Smith"}) syntax
is used to remove a document from the faculty collection.
Comments
Post a Comment