MongoDB is a NoSQL, document based database. It is free and open-source software. In this article, we will come to know the basic units of MongoDB and its setup.
MongoDB Introduction
We all know a relation database consists of mostly tables and views. Mongo database on other hand, consists of collections, a collection is analogous to a relational database’s table. Each collection contains a set of MongoDB documents. You can think of document as a table’s row. Each document in turn is composed of key-value pairs, similar to JSON objects.
Each document must have a unique _id field that acts as a primary key.
MongoDB Setup
MongoDB runs on various platforms and supports both 32-bit and 64-bit architectures. You need to first download the correct version of MongoDB based on your platform.
-
Here we will be setting up MongoDB on windows 7, 64-bit version.
Download MongoDB. - Next, run the downloaded MSI installer file. In case you want to chose specific download location other than the default one, you may chose the ‘Custom’ way, provide directory path where you want the downloaded files. We have installed mongoDB in
c:\mongoDB
. - Now we need to create a data directory where the database files of MongoDB will reside. Create a data directory
data
within the the mongoDB directory. - To start MongoDB, run
C:\mongodb\bin>mongod.exe --dbpath "C:\mongodb\data"
- Once started, you should see ‘Waiting for connections on port’ as the last line in console.
C:\mongodb\bin>mongod.exe --dbpath "C:\mongodb\data" 2015-12-31T12:52:14.324+0530 I CONTROL [main]Hotfix KB2731284 or later update is not installed, will zero-out data files 2015-12-31T12:52:14.328+0530 I CONTROL [initandlisten]MongoDB starting : pid=1 4468 port=27017 dbpath=C:\mongodb\data 64-bit host=INMAA1-L1005 2015-12-31T12:52:14.329+0530 I CONTROL [initandlisten]targetMinOS: Windows 7/W indows Server 2008 R2 2015-12-31T12:52:14.329+0530 I CONTROL [initandlisten]db version v3.2.0 2015-12-31T12:52:14.329+0530 I CONTROL [initandlisten]git version: 45d947729a0 315accb6d4f15a6b06be6d9c19fe7 2015-12-31T12:52:14.329+0530 I CONTROL [initandlisten]OpenSSL version: OpenSSL 1.0.1p-fips 9 Jul 2015 2015-12-31T12:52:14.329+0530 I CONTROL [initandlisten]allocator: tcmalloc 2015-12-31T12:52:14.329+0530 I CONTROL [initandlisten]modules: none 2015-12-31T12:52:14.329+0530 I CONTROL [initandlisten]build environment: 2015-12-31T12:52:14.329+0530 I CONTROL [initandlisten]distmod: 2008plus-ss l 2015-12-31T12:52:14.329+0530 I CONTROL [initandlisten]distarch: x86_64 2015-12-31T12:52:14.329+0530 I CONTROL [initandlisten]target_arch: x86_64 2015-12-31T12:52:14.329+0530 I CONTROL [initandlisten]options: { storage: { db Path: "C:\mongodb\data" } } 2015-12-31T12:52:14.378+0530 I - [initandlisten]Detected data files in C :\mongodb\data created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'. 2015-12-31T12:52:14.379+0530 I STORAGE [initandlisten]wiredtiger_open config: create,cache_size=8G,session_max=20000,eviction=(threads_max=4),config_base=fals e,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snapp y),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),stati stics_log=(wait=0),direct_io=(data), 2015-12-31T12:52:15.295+0530 I NETWORK [HostnameCanonicalizationWorker] Startin g hostname canonicalization worker 2015-12-31T12:52:15.295+0530 I FTDC [initandlisten]Initializing full-time d iagnostic data capture with directory 'C:/mongodb/data/diagnostic.data' 2015-12-31T12:52:15.300+0530 I NETWORK [initandlisten]waiting for connections on port 27017
MongoDB Shell
Now that we have installed MongoDB let’s interact with it using ‘Mongo Shell’ command line program.
C:\mongodb\bin>mongo 2015-12-31T13:00:25.617+0530 I CONTROL [main]Hotfix KB2731284 or later update is not installed, will zero-out data files MongoDB shell version: 3.2.0 connecting to: test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user <
Insert Document using MongoDB Shell
Let’s insert a sample document using MongoDB shell. We will create documents in database called mydb
.
In order to do so, first we need to switch the database. By default when you run ‘mongo’ shell, MongoDB will use ‘test’ database.
> use mydb switched to db mydb
Databases and collections are first created when documents are actually inserted so even though we have switched over the database to use mydb
, mydb
is not yet created.
< show dbs admin 0.000GB complexIdTest 0.000GB database 0.001GB issue 0.000GB local 0.000GB mongo-index-db1 0.000GB mongoRepositoryTextSearchIntegrationTests 0.000GB repositories 0.000GB script-tests 0.000GB test 0.000GB validation 0.000GB
You can insert a document using db.{collection name}.insert
. Below statement inserts some product details, collection name is product
.
< db.product.insert({"product": "Comfi Storage Bed", "finish": "teak", "size": "king", "price": 42999, "desc": "Bring home the style"})
After insert, the write result shows up:
WriteResult({ "nInserted" : 1 })
You can fetch the inserted documents using db.{collection name}.find()
.
{ "_id" : ObjectId("5684ef074e82c53109540b7c"), "product" : "Comfi Storage Bed", "finish" : "teak", "size" : "king", "price" : 42999, "desc" : "Bring home the style" }
You would have noticed that the _id field has been added to the document. This acts as a primary key and since we haven’t provided one, mongoDB generated it.