MongoDB is an open-source document database that provides high performance, high availability, and automatic scaling.
MongoDB stores data in flexible BSON documents meaning fields can vary from document to document and data structure can be changed over time. BSON is a binary representation of JSON documents, though it contains more data types than JSON.
Let’s understand the terms used in MongoDB-
- Database –
- A database is an organized collection of data.
- A physical container for collections. Each database gets its own set of files on the file system.
- MongoDB databases hold collections of documents.
- Collection –
- Collection is a group of MongoDB documents.
- It is the equivalent of an RDBMS table. A collection exists within a single database.
- Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection are of similar or related purpose.
- MongoDB collections hold data records i.e BSON documents.
- Documents –
- A record in a MongoDB collection.
- Documents are analogous to JSON objects(but exist in the database in a more type-rich format known as BSON).
- MongoDB stores data records as BSON documents. BSON is a binary representation of JSON documents, though it contains more data types than JSON
- Documents have dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection’s documents may hold different types of data.
- MongoDB documents is a set of key-value pairs and basic unit of data.
Mongo.exe => The
mongo shell to query and update data as well as perform administrative operations. we can start the mongo shell by using the mongo command in the command prompt.
- High Performance – MongoDB provides high performance data persistence. In particular-
- Support for embedded data models reduces I/O activity on database system.
- Indexes support faster queries and can include keys from embedded documents and arrays. Rich Query Language
- Rich Query Language – MongoDB supports a rich query language to support read and write operations (CRUD) as well as:
- High Availability – MongoDB’s replication facility, called replica set(A replica set is a group of MongoDB servers that maintain the same data set, providing redundancy and increasing data availability.), provides:
- automatic failover and
- data redundancy.
- Horizontal Scalability – MongoDB provides horizontal scalability as part of its core functionality:
- Sharding distributes data across a cluster of machines.
- MongoDB 3.4 supports creating zones of data based on the shard key. In a balanced cluster, MongoDB directs reads and writes covered by a zone only to those shards inside the zone. See the Zones manual page for more information.
- Support for Multiple Storage Engines – MongoDB supports multiple storage engines, such as:
- WiredTiger Storage Engine
- MMAPv1 Storage Engine.
- Download and Install latest version from – https://docs.mongodb.com/manual/installation/
- Let’s assume default installation directory is – C:\Program Files\MongoDB\Server\3.4\bin
- Set Env Path variable to – C:\Program Files\MongoDB\Server\3.4\bin
- Now check the version using below command to ensure that path is setup correctly- C:\Users\jogen>mongo -version
- MongoDB requires a data folder to store its files. The default location for the MongoDB data directory is “C:\data\db”
- If you want to store the data files at different location so we can set the preferred location path using –dbpath as below then it will show waiting for connections message on the console output, which indicates that the mongod.exe process is running successfully. If this path contains spaces then put all its path in double qoutes.
- C:\Users\jogen>mongod –dbpath “D:\MongoDbFiles\data”
- Set log path – used by mongo daemon process,for flush out logs instead of in standard console as-
- C:\Users\jogen>mongod –logpath “D:\MongoDbFiles\logs\mongo.logs” –logappend
- Set port using –port, default is- 27017
We can also specify above configuration option using a config file. Config file is just text file, containing the parameters in the key=value form and each one is specified on the every line of file. We basically provide file path (which contains the configurations) as command line option as “-f” or “–config” as below-
- C:\Users\jogen>mongod –f D:\Servers\mongodb\config\mongodb.conf
- C:\Users\jogen>mongod –config D:\Servers\mongodb\config\mongodb.conf
Start/Stop MongoDB using the Windows services –
MongoDB also comes with excellent support to turn your MongoDB server into a windows service that gets launched automatically at startup.
- –install : command line switch to install the service
- –remove : command line switch to remove the service
- –serviceName : the name for mongod windows service and must adhere to naming services in windows like only accepting the alphanumeric chars with no spaces
- –serviceDisplayName : Display name for service that is shown in services console, put this in double quotes if contains the spaces
- –serviceDescription : Small description about service, put this in double quotes if contains the spaces
To do this open command prompt with admin rights and fire below on the shell-
mongod –dbpath “D:\MongoDbFiles\data” –logpath “D:\MongoDbFiles\logs\mongo.logs” –logappend –install –serviceName mdb27017 –serviceDisplayName “MongoDB Server Instance 27017” –serviceDescription “MongoDB Server Instance running on 27017”
In log path specified, you can check for the whether windows services started or not or look into services.msc for –
GUI tool for MongoDB –
Robo 3T (formerly Robomongo) is the free lightweight GUI for MongoDB enthusiasts.
Programming is Easy…