Skip to content

Latest commit

 

History

History
160 lines (147 loc) · 4.44 KB

6.neo4j-mongodb.md

File metadata and controls

160 lines (147 loc) · 4.44 KB

Neo4j and MongoDB

Neo4j and MongoDb are both database management systems. A database management system (DBMS) is a software made for storing, organizing, accessing, adding, updating, and removing data efficiently, securely, and resourcefully.

Neo4j is a graph-oriented database management system, which means it stores data in the form of nodes in a graph, connected by edges represending relationships between them.

MongoDB is a document-oriented database management system, which allows us to store data in the form of BSON objects, a data structure quite similar to JSON but optimized and extended.

A recording of the session demonstrations is available here.

Neo4j: Exercise

Neo4j uses Cypher as its query language. In this exercise, you will be writing queries in Cypher to get a glimpse of how Neo4j works.

  1. Navigate to the Neo4j Console.
  2. Get rid of the existing sample database either by clicking 'Clear DB' or by executing the following query:
    MATCH
    	(n)
    DETACH DELETE n
  3. Create a new graph using the following query:
    CREATE 
    	(a:Person:Male {name: 'Param'}),
    	(b:Person:Male {name: 'Ritesh'}),
    	(c:Person:Male {name: 'Mukul'}),
    	(d:Person:Male {name: 'Kushan'}),
    	(e:Person:Female {name: 'Vani'}),
    	(f:Person:Female {name: 'Krati'}),
    	(g:Person:Female {name: 'Khushi'}),
    	(h:Thing {colour: 'Red', edible: true}),
    	(i:Thing {colour: 'Blue', edible: false}),
    	(j:Thing {colour: 'Yellow', edible: true}),
    
    	(a)-[:Likes]->(b),
    	(b)-[:Likes]->(a),
    	(d)-[:Likes]->(c),
    	(c)-[:Likes]->(e),
    	(e)-[:Likes]->(d),
    	(e)-[:Likes]->(f),
    	(f)-[:Likes]->(e),
    	(f)-[:Likes]->(g),
    	(g)-[:Likes]->(f),
    	(e)-[:Likes]->(g),
    	(c)-[:Likes]->(g),
    	(g)-[:Likes]->(c),
    	(f)-[:Likes]->(c),
    
    	(a)-[:Eats]->(h),
    	(a)-[:Eats]->(i),
    	(g)-[:Eats]->(j),
    	(d)-[:Eats]->(j),
    	(b)-[:Eats]->(j)
    Observe the resulting graph. There are persons and things, each person having a name, and each thing being identified by its colour and whether or not it is edible.
  4. Use the following query to find our which pairs of persons like each-other and can be friends:
    MATCH
    	(a:Person),
    	(b:Person)
    WHERE
    	ID(a) < ID(b)
    	AND (a)-[:Likes]->(b)-[:Likes]->(a)
    RETURN a.name AS `Friend 1`, b.name AS `Friend 2`
    We are using ID(a) < ID(b) only to make sure we don't get symmetric pairs, such as (x, y) and (y, x).

The above exercise gives you a good idea of how graph databases work and why they are useful. It also teaches you some of Cypher (Neo4j) syntax.

MongoDB: Exercise

MongoDB uses the MongoDB Query Language (MQL), which shares most of its syntax with JavaScript. The following exercises will give you an opportunity to write queries in MQL to work with a MongoDB database.

  1. Navifate to the MongoDB Playground. The default collection will be named 'collection'.
  2. Copy-paste the following JavaScript snippet into the 'Configuration':
    db = {
    	people: [
    		{
    			name: "Shikhar",
    			age: 16,
    			hungry: false
    		},
    		{
    			name: "Swapnil",
    			age: 17,
    			hungry: true
    		},
    		{
    			name: "Soumya",
    			age: 18,
    			hungry: false
    		},
    		{
    			name: "Ankur",
    			age: 19,
    			hungry: false
    		},
    		{
    			name: "Astha",
    			age: 20,
    			hungry: true
    		},
    		{
    			name: "Prachur",
    			age: 21,
    			hungry: true
    		}
    	]
    };
  3. Execute the following query to find people who are older than 17 years of age and are not hungry right now:
    db.people.find({
    	age: {
    		$gt: 17
    	},
    	hungry: false
    });
    Examine the output. You will notice each document has an _id field assigned. This field uniquely identifies each document in a collection.
  4. Execute the following query to find people who are older than 17 years of age and are not hungry right now:
    db.people.find({
    	age: {
    		$gt: 17
    	},
    	hungry: false
    });
    Examine the output. You will notice each document has an _id field assigned. This field uniquely identifies each document in a collection.

The above exercise gives you an idea about using MQL to query a MongoDB database and work with collections and documents.

Remarks

  • The abovementioned database technologies are widely-used and popular.
  • When connecting with applications, we don't usually write the queries ourselves but use connector modules.
  • Carefully examine the queries being used to properly understand what is happening.

Made with ❤ by Param.