Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing MongoDB #16

Closed
alonttal opened this issue Nov 1, 2017 · 7 comments
Closed

Introducing MongoDB #16

alonttal opened this issue Nov 1, 2017 · 7 comments

Comments

@alonttal
Copy link
Collaborator

alonttal commented Nov 1, 2017

Introduction to MongoDB

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.
@AdiOmari
Copy link

AdiOmari commented Nov 6, 2017

@alonttal You did a great job!

@OrAbramovich
Copy link
Collaborator

I started reading more about the MongoDB (since it seems as the most suitable DB) - I agree with a argument I read that working directly with the Java driver it’s quite low-level - we have to do a lot of taking things out of our domain objects and poking them into MongoDB-shaped DBObjects, and vice-versa (it can be implemented with an adapter design pattern but it is still a "low-level" work).

The extra work has been done before by others. There are tools that use the MongoDB Java Driver at their core to interact with MongoDB. They provide a high-level abstraction for converting the domain objects into MongoDB documents, whilst also giving you a way to get to the underlying driver as well in case you need to use it at a lower level.

One of them is Spring Data. Spring Data is an ODM (Object Document Mapper). This supports traditional relational and non-relational databases, including MongoDB and since we are going to use Spring in our application, this should be a familiar way to work.

In addition, there’s a lot of really great documentation, including a Getting Started guide with example code - I will summarize it (the section of working with the DB).

@OrAbramovich
Copy link
Collaborator

I collected the information I read and wrote a ~20 pages tutorial about: MongoDB and the integration of it with Java (with Java Driver and Spring Data) - The tutorial (which is mostly copy-pasted) covers the core ideas of the above issues alongside practical information relevant to our interests (e.g handling geolocation data, installing on Amazon Linux platform…)

The tutorial includes information about -

  • Overview
  • Installation
  • Querying
  • Etc

The tutorial is attached as a pdf file (the version attached here is the first one and it will be updated during the work).
MongoDB Tutorial.pdf

@alonttal
Copy link
Collaborator Author

wow @OrAbramovich great work !! thanks !

@OrAbramovich
Copy link
Collaborator

Thanks!

@chananbental
Copy link
Collaborator

a short comparision between MySQL and MongoDB
MongoDB vs MySQL.pdf

@alonttal
Copy link
Collaborator Author

moved this tutorial to "Guides" section in Wiki

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants