With
NoSQL databases
Types of NoSQL stores:
- Document store: data are organized as a collection of documents, e.g. MongoDB, CouchDB;
- Key-value store: data are stored as key-value pairs without predefined schema, e.g. Apache Cassandra, Dynamo, Hbase, Amazon SimpleDB;
- Graph-based store: data are stored in graph structures with nodes, edges, and properties, e.g. Neo4j, InfoGrid, Horton.
MongoDB compared to relational databases:
RDBMS | MongoDB | |
---|---|---|
Database | ←→ | Database |
Table | ←→ | Collection |
Row | ←→ | Document |
Column | ←→ | Field |
MongoDB installation
MongoDB Atlas on the cloud https://www.mongodb.com/cloud/atlas
MongoDB Community Server https://www.mongodb.com/download-center/community
- On Windows, download and install with the MSI installer
- On Mac OS (recommended method with HomeBrew):
brew tap mongodb/brew
brew install mongodb-community@4.2
Command line:
mongo
>
At the mongo
prompt, use mongodb commands:
> show dbs
to show a list of databases
A MongoDB database
a physical container for document collections
You can open a database by:
> use DatabaseName
Show the list of collections in the database:
> show collections
A MongoDB collection is
a group of documents, similar to a table in a relational database
- Can be split across multiple shards
- Sharding for horizontal scaling
A MongoDB document
is a record and implements a schema-less model
- Documents do not have to have same fields (as in relational databases)
- Format similar to JSON (Javascript Object Notation)
- Binary representation with BSON
There is a MongoDB web shell, similar to the command line mongo
shell, which supports mongo operations in the browser.
Mongo Commands
Now let's go and test some of the features in MongoDB. Make sure:
- First command line window:
mongod
has been started and running. - Second command line window: run
mongo
to start the mongo shell.
Note: You can connect to a different server such as:
mongo --host mongodb0.example.com --port 28015
Example screenshot of two command line windows on Mac:
show dbs
Now you can switch to an existing database or a new database:
use college
db.students.insertOne(
{
name: "John Smith",
program: "Data Science",
class_year: 2024
}
)
db.students.insertOne(
{
name: "Sue Anderson",
program: "Data Science",
class_year: 2020
}
)
db.students.insertOne(
{
name: "Dylan Johnson",
program: "Computer Science",
class_year: 2019
}
)
db.students.insertOne(
{
name: "Paul Sanderson",
program: "Information Science",
class_year: 2021
}
)
For a new database -- for example, the college
database does not exist when we switch to it -- the system will automatically create it when the a piece of data is stored to it. In this case, the insertOne()
opereation will create the college
database, add the students
collection, and create a new student record with the provided data.
db.students.insertOne(
{
name: "Mary Brown",
program: "Information Science",
age: 20
}
)
Although the students
collection is like a table, there is predefine structure or schema. You can insert another document with a differetn set of fields.
db.students.updateMany(
{ age: { $gt: 150} },
{ $set: { status: "reject"} }
)
db.students.deleteMany(
{ status: "reject" }
)
db.students.find(
{ class_year: { $lt: 2021} },
{ name: 1, program: 1}
)
The first part of the query defines the filter condition, i.e. class_year $< 2021$, and the second part lists the fields to be shown in the result.
- Selecting all documents from the collection:
db.students.find()
- Getting the number of found documents:
db.students.find().count()
- Find documents based on a field value
db.students.find({"program":"Data Science"})
- Find one document based on field value
db.students.findOne({"program":"Data Science"})
db.students.find({"program":"Data Science"}).explain()
The explain method tests query operation and report information about the query execution.
References
- Chapter 12 Data Processing and Aggregation with MongoDB, of Hector Cuesta (2013). Practical Data Analysis. https://ebookcentral-proquest-com.ezproxy2.library.drexel.edu/lib/drexel-ebooks/detail.action?docID=1507840