MongoDB (Cross-platform Document-Oriented Database)
MongoDB is a NoSQL database where each record is a document comprising of key-value pairs that are similar to JSON objects with schemas. MongoDB is flexible and allows its users to create schema, databases, tables, etc.
Documents that are identifiable by a primary key make up the basic unit of MongoDB. Once MongoDB is installed, users can make use of Mongo shell as well. Mongo shell provides a JavaScript interface through which the users can interact and carry out CRUD operations.
In other words we can say that MongoDB is a general purpose, document-based, distributed database built for modern application developers and for the cloud era licensed under the Server Side Public License.
Why use MongoDB?
- Scalable and can serve several machines.
- It is JavaScript Based which makes it easier to use.
- Has Faster response because it is a document-oriented database.
- Simpler Environment Setup
- It uses JSON syntax which is very easy to use and has a wide range of browser compatibility.
- Data is stored in the form of JSON whether it is Objects, Object Members, Arrays, Values and Strings.
- Uber and Stack Companies uses MongoDB!
Writing queries in MongoDB
Let’s look at some common queries in MongoDB
- Creating a student database
use student;
- Creating a students table
db.createCollection("students");
- Inserting records into the students collection
db.students.insert
(
{
"id" : 1,
"Name" : "Harry",
"Team": "Geeks For Geeks"
}
);
- Reading from the students collection
db.students.find({Name : "Harry"}).forEach(printjson);
Difference between PostgreSQL and MongoDB
The main difference between PostgreSQL and MongoDB is that PostgreSQL is a relational database management system that uses SQL, whereas MongoDB is a non-relational, document-oriented database that stores data in flexible, JSON-like documents.