Skip to content

Introduction to MongoDB

Alon Talmor edited this page Nov 19, 2017 · 4 revisions

made by @alonttal

Why MongoDB?

  • Open-source document database and leading NoSQL database. document database is a database designed to store data in a semi-structure format, typically in JSON or XML. In MongoDB, the information is stored in JSON format.
  • JSON document support makes life easier for developers to serialize and load objects, so coding is done on higher (than RSBMS DBs) abstraction.
  • No frustrating joins.
  • Ease of scale-out.
  • All this leads to faster, more agile development!

How Does MongoDB Stores Data?

  • First layer – database: the database is spread out among several files and holds as a physical container for collections.
  • Second layer - collection: Collection is the equivalent of table in the traditional RDBMS, but opposed to that it does not enforce a fix schema. Documents within a collection can have different fields (useful?).
  • Third layer - Document: a set of key-value pairs, equivalent of RDBMS' row.

Official Drivers

MongoDB is available in C++, C#, Java, Node.js and more… See: https://docs.mongodb.com/ecosystem/drivers/

Data Modeling

As already said, the data is saved in JSON format and documents in the same collection do not need to have the same structure. For example, let's write a small collection of apartments:

{
	_id: APARTMENT_ID
	author: USER_1
	picture: 'house.jpg'
	 price: 1500
	address: 
	  {
		city: 'HAIFA', 
		street: 'TRUMPELDOR'
		number: 10
	  }
	description:  'looking for another friendly roommate for the best apartment in town!'
	likes: 2
	tags: [AIR_CONDITION, NO_SMOKING, NO_PETS]
	comments: [
	  {
		User: USER_2
		Message: 'how far is it from the closest market?'
	  },
	  {
		User: USER_1,
		Message: '2 minutes by foot'
	  }
	]
}

As we can see, all the information can be stored in a single collection, while our RDBMS fellow needs: a table to store apartments data, a table to store comments, and a table to store tags. Was it convincing enough?

Some Features

  • Insert document to collection: each object is automatically given a unique _id. db.COLLECTION_NAME.insert(document)
  • Update document: all documents with CRITERIA will be updated. It is also easily possible to update the first occurrence. db.COLLECTION_NAME.update(CRITERIA, UPDATED_VALUE) Delete document: all documents with CRITERIA will be removed. Using justOne() will remove the first occurrence with CRITERIA. db.COLLECTION_NAME.remove(CRITERIA) db.COLLECTION_NAME.remove(CRITERIA).justOne()
  • Find document: find will display all documents. Optionally add CRITERIA to be more specific. Using binary operators is also possible. db.COLLECTION_NAME.find([CRITERIA])
  • Sorting documents: will sort by document FIELD_NAME in ascending order. Db.COLLECTION_NAME.find().sort({"FIELD_NAME":1})
  • Aggregate: process documents and return computed results. Includes sum, avg, min, max, etc. for example, let's say we have a collection of apartments and we want to find the cheapest one.
[
{
	…
	Price: 1300
},
{
	…
	Price: 2000
},
…
]

In order to do so, we will write: db.apartments.aggregate([{$group : {_id : "$_id", min_price : {$min : "$price"}}}])

  • Create backup: mongodump
  • There is a lot more, meanwhile I'll keep it simple…

@OrAbramovich comment on MongoDB:

  • License: MongoDB is available at no cost under the GNU Affero General Public License.
  • Storing geo-location data: has native support: MongoDB has built in spacial functions, so finding relevant data from specific locations is fast and accurate. In MongoDB, we can store geospatial data as GeoJSON objects or as legacy coordinate pairs (https://docs.mongodb.com/manual/geospatial-queries/).
  • Performance: this DB has better performance than RDBMS DBs because by default prefers high insert rate over transaction safety.
  • Other features: supports replication as well.
  • Dependencies & minimal system requirements: cross-platform.
Clone this wiki locally