MongoDB

MongoDB is a document oriented database that stores collections and documents.

  • Collections – group of documents [ {d1}, {d2}, {d3}, {dn} ]
  • Documents – key value pairs with a dynamic schema. {key : value}

Below given table shows the relationship of RDBMS terminology with MongoDB

RDBMS MongoDB
Database Database
Table Collection
Tuple/Row Document
column Field
Table Join Embedded Documents
Primary Key Primary Key (Default key _id provided by mongodb itself)

 

The Data Model

In RDBMS schema design for above requirements will have minimum three tables.

RDBMS Schema Design

While in MongoDB schema design will have one collection post and has the following structure:

mongodb ubuntu

{
   _id: POST_ID
   title: TITLE_OF_POST, 
   description: POST_DESCRIPTION,
   by: POST_BY,
   url: URL_OF_POST,
   tags: [TAG1, TAG2, TAG3],
   likes: TOTAL_LIKES, 
   comments: [	
      {
         user:'COMMENT_BY',
         message: TEXT,
         dateCreated: DATE_TIME,
         like: LIKES 
      },
      {
         user:'COMMENT_BY',
         message: TEXT,
         dateCreated: DATE_TIME,
         like: LIKES
      }
   ]
}

So while showing the data, in RDBMS you need to join three tables and in mongodb data will be shown from one collection only.

References

References store the relationships between data by including links or references from one document to another. Applications can resolve these references to access the related data. Broadly, these are normalized data models.

Data model using references to link documents. Both the ``contact`` document and the ``access`` document contain a reference to the ``user`` document.

In general, use normalized data models:

  • when embedding would result in duplication of data but would not provide sufficient read performance advantages to outweigh the implications of the duplication.
  • to represent more complex many-to-many relationships.
  • to model large hierarchical data sets.

References provides more flexibility than embedding. However, client-side applications must issue follow-up queries to resolve the references. In other words, normalized data models can require more round trips to the server.

Embedded Data

Embedded documents capture relationships between data by storing related data in a single document structure. MongoDB documents make it possible to embed document structures in a field or array within a document. These denormalized data models allow applications to retrieve and manipulate related data in a single database operation.

Data model with embedded fields that contain all related information.

In general, use embedded data models when:

Getting Started

  1. Install MongoDB from https://www.mongodb.org/downloads#production
  2. Create /data/db for mongoDB data folder.
  3. Run /mongo/bin/mongod to start MongoDB on port 27017
  4. Run mongo to run the client

On Ubuntu the MongoDB instance stores its data files in /var/lib/mongodb and its log files in /var/log/mongodb by default, and runs using the mongodb user account. You can specify alternate log and data file directories in /etc/mongod.conf. See systemLog.path and storage.dbPath for additional information. To run:

sudo service mongod start

Verify that the mongod process has started successfully by checking the contents of the log file at /var/log/mongodb/mongod.log for a line reading

[initandlisten] waiting for connections on port <port>
where <port> is the port configured in /etc/mongod.conf, 27017 by default.

Type ./bin/mongo to start mongo:

Commands

use DATABASE_NAME
Create database or use existing databse if it exists

show dbs
Show list of databases

db.dropDatabase()
drop a existing database

show collection
Show list of collections

db.createCollection(name, options)
name is name of collection to be created. Options is a document and used to specify configuration of collection. MongoDB creates collection automatically, when you insert some document.

db.collection.drop()
drop a collection from the database

db.COLLECTION_NAME.insert(document)
Insert data into MongoDB collection. if we don’t specify the _id parameter, then MongoDB assigns an unique ObjectId for this document.

The components of a MongoDB insert operations.

Note:

During the insert, mongod will create the _id field and assign it a unique ObjectId value, as verified by the inserted document:

{“_id”:ObjectId(“5063114bd386d8fadbd6b004”),“item”:“card”,“qty”:15}

The ObjectId values are specific to the machine and time when the operation is run. As such, your values may differ from those in the example.

Insert a Document Specifying an _id Field

In the following example, the document passed to the insert() method includes the _id field. The value of _id must be unique within the collection to avoid duplicate key error.

db.products.insert({_id:10,item:’box’,qty:20})

Insert Multiple Documents

The following example performs a bulk insert of three documents by passing an array of documents to the insert() method. By default, MongoDB performs an ordered insert. With ordered inserts, if an error occurs during an insert of one of the documents, MongoDB returns on error without processing the remaining documents in the array.

The documents in the array do not need to have the same fields. For instance, the first document in the array has an _id field and a type field. Because the second and third documents do not contain an _id field, mongod will create the _id field for the second and third documents during the insert:

db.products.insert([{_id:11,item:’pencil’,qty:50,type:’no.2′},{item:’pen’,qty:20},{item:’eraser’,qty:25}])

The operation inserted the following three documents:

{_id:11,item:’pencil’,qty:50,type:’no.2′}
{_id:ObjectId(‘51e0373c6f35bd826f47e9a0′),item:’pen’,qty:20}
{_id:ObjectId(‘51e0373c6f35bd826f47e9a1′),item:’eraser’,qty:25}

 

db.COLLECTION_NAME.find()
display all the documents in a non structured way.

The components of a MongoDB find operation.

db.mycol.find().pretty()
display the results in a formatted way, you can use pretty() method.

db.mycol.findOne()
method, that retuns only one document

 

RDBMS Where Clause Equivalents in MongoDB

To query the document on the basis of some condition, you can use following operations

Operation Syntax Example RDBMS Equivalent
Equality {<key>:<value>} db.mycol.find({“by”:”tutorials point”}).pretty() where by = ‘tutorials point’
Less Than {<key>:{$lt:<value>}} db.mycol.find({“likes”:{$lt:50}}).pretty() where likes < 50
Less Than Equals {<key>:{$lte:<value>}} db.mycol.find({“likes”:{$lte:50}}).pretty() where likes <= 50
Greater Than {<key>:{$gt:<value>}} db.mycol.find({“likes”:{$gt:50}}).pretty() where likes > 50
Greater Than Equals {<key>:{$gte:<value>}} db.mycol.find({“likes”:{$gte:50}}).pretty() where likes >= 50
Not Equals {<key>:{$ne:<value>}} db.mycol.find({“likes”:{$ne:50}}).pretty() where likes != 50

AND in MongoDB

In the find() method if you pass multiple keys by separating them by ‘,’ then MongoDB treats it AND condition. Basic syntax of AND is shown below −

>db.mycol.find({key1:value1, key2:value2}).pretty()

Below given example will show all the tutorials written by ‘tutorials point’ and whose title is ‘MongoDB Overview’

>db.mycol.find({“by”:”tutorials point”,”title”: “MongoDB Overview”}).pretty() {    “_id”: ObjectId(7df78ad8902c),    “title”: “MongoDB Overview”,     “description”: “MongoDB is no sql database”,    “by”: “tutorials point”,    “url”: “http://www.tutorialspoint.com”,    “tags”: [“mongodb”, “database”, “NoSQL”],    “likes”: “100” } >

For the above given example equivalent where clause will be ‘ where by=’tutorials point’ AND title = ‘MongoDB Overview’ ‘. You can pass any number of key, value pairs in find clause.

 

OR in MongoDB

To query documents based on the OR condition, you need to use $or keyword. Basic syntax of OR is shown below −

>db.mycol.find( { $or: [ {key1: value1}, {key2:value2} ] } ).pretty()

Below given example will show all the tutorials written by ‘tutorials point’ or whose title is ‘MongoDB Overview’

>db.mycol.find({$or:[{“by”:”tutorials point”},{“title”: “MongoDB Overview”}]}).pretty() {    “_id”: ObjectId(7df78ad8902c),    “title”: “MongoDB Overview”,     “description”: “MongoDB is no sql database”,    “by”: “tutorials point”,    “url”: “http://www.tutorialspoint.com”,    “tags”: [“mongodb”, “database”, “NoSQL”],    “likes”: “100” } >

 

Using AND and OR together

Below given example will show the documents that have likes greater than 100 and whose title is either ‘MongoDB Overview’ or by is ‘tutorials point’. Equivalent sql where clause is ‘where likes>10 AND (by = ‘tutorials point’ OR title = ‘MongoDB Overview’)’

>db.mycol.find({“likes”: {$gt:10}, $or: [{“by”: “tutorials point”},    {“title”: “MongoDB Overview”}]}).pretty() {    “_id”: ObjectId(7df78ad8902c),    “title”: “MongoDB Overview”,    “description”: “MongoDB is no sql database”,    “by”: “tutorials point”,    “url”: “http://www.tutorialspoint.com”,    “tags”: [“mongodb”, “database”, “NoSQL”],    “likes”: “100” } >

 

db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA)

The components of a MongoDB update operation.
method updates values in the existing document.
db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}})
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"New MongoDB Tutorial"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
>

By default mongodb will update only single document, to update multiple you need to set a paramter ‘multi’ to true.

db.mycol.update({'title':'MongoDB Overview'},
   {$set:{'title':'New MongoDB Tutorial'}},{multi:true})

db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})
replaces the existing document with the new document passed in save() method

Following example will replace the document with the _id ‘5983548781331adf45ec7’

>db.mycol.save(
   {
      "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point New Topic",
         "by":"Tutorials Point"
   }
)
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"Tutorials Point New Topic",
   "by":"Tutorials Point"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
>

 

db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)

The components of a MongoDB remove operation.
used to remove document from the collection. remove() method accepts two parameters. One is deletion criteria and second is justOne flag
  1. deletion criteria : (Optional) deletion criteria according to documents will be removed.
  2. justOne : (Optional) if set to true or 1, then remove only one document.

db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
Remove only one. If there are multiple records and you want to delete only first record, then set justOne parameter in remove() method

db.mycol.remove()
If you don’t specify deletion criteria, then mongodb will delete whole documents from the collection. This is equivalent of SQL’s truncate command.

db.COLLECTION_NAME.find({},{KEY:1})
projection meaning is selecting only necessary data rather than selecting whole of the data of a document. Accepts second optional parameter that is list of fields that you want to retrieve. In MongoDB when you execute find() method, then it displays all fields of a document. To limit this you need to set list of fields with value 1 or 0. 1 is used to show the field while 0 is used to hide the field.

db.COLLECTION_NAME.find().limit(NUMBER)
limit the records in MongoDB, you need to use limit() method. limit() method accepts one number type argument, which is number of documents that you want to displayed.

db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
accepts number type argument and used to skip number of documents.default value in skip() method is 0

db.COLLECTION_NAME.find().sort({KEY:1})
accepts a document containing list of fields along with their sorting order. To specify sorting order 1 and -1 are used. 1 is used for ascending order while -1 is used for descending order.

db.COLLECTION_NAME.ensureIndex({KEY:1})
Indexes support the efficient resolution of queries. Without indexes, MongoDB must scan every document of a collection to select those documents that match the query statement. This scan is highly inefficient and require the mongodb to process a large volume of data. Indexes are special data structures, that store a small portion of the data set in an easy to traverse form. The index stores the value of a specific field or set of fields, ordered by the value of the field as specified in index. Here key is the name of filed on which you want to create index and 1 is for ascending order. To create index in descending order you need to use -1.

 

db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)

Aggregations operations process data records and return computed results. Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result. In sql count(*) and with group by is an equivalent of mongodb aggregation.
See http://www.tutorialspoint.com/mongodb/mongodb_aggregation.htm

Diagram of the annotated aggregation pipeline operation. The aggregation pipeline has two stages: ``$match`` and ``$group``.

 

db.COLLECTION_NAME.mapReduce(REDUCE_FUNCTION)

MongoDB also provides map-reduce operations to perform aggregation. In general, map-reduce operations have two phases: a map stage that processes each document and emits one or more objects for each input document, and reduce phase that combines the output of the map operation.

Diagram of the annotated map-reduce operation.

Single Purpose Aggregation Operations

MongoDB also provides db.collection.count(), db.collection.group(), db.collection.distinct(). special purpose database commands.

Diagram of the annotated distinct operation.

 

CSV Import

In the following example, mongoimport imports the csv formatted data in the/opt/backups/contacts.csv file into the collection contacts in the users database on the MongoDB instance running on the localhost port numbered 27017.

Specifying –headerline instructs mongoimport to determine the name of the fields using the first line in the CSV file.

mongoimport --db users --collection contacts --type csv --headerline --file /opt/backups/contacts.csv

mongoimport uses the input file name, without the extension, as the collection name if -c or --collection is unspecified.

Use the “–ignoreBlanks” option to ignore blank fields.

Import CSV to Remote Host Running with Authentication

Username, password and authenicationDatabase options are needed:

mongoimport -d donorschoose -c projects --type csv --headerline --file D:\TESTCodeNodeJs\node-dc-mongo-master\sampledata\sampledata.csv --username admin --password password --authenticationDatabase admin

 

Connecting to MongoDb

 

References