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 uses Cypher as its query language. In this exercise, you will be writing queries in Cypher to get a glimpse of how Neo4j works.
- Navigate to the Neo4j Console.
- Get rid of the existing sample database either by clicking 'Clear DB' or by
executing the following query:
MATCH (n) DETACH DELETE n
- Create a new graph using the following query:
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.
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)
- Use the following query to find our which pairs of persons
like each-other and can be friends:
We are using
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`
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 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.
- Navifate to the MongoDB Playground. The default collection will be named 'collection'.
- 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 } ] };
- Execute the following query to find people who
are older than 17 years of age and are not hungry
right now:
Examine the output. You will notice each document has an
db.people.find({ age: { $gt: 17 }, hungry: false });
_id
field assigned. This field uniquely identifies each document in a collection. - Execute the following query to find people who
are older than 17 years of age and are not hungry
right now:
Examine the output. You will notice each document has an
db.people.find({ age: { $gt: 17 }, hungry: false });
_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.
- 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.