Skip to content

martinharnik/MongoDB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

MongoDB and NoSQL Concepts

NoSQL Databases

NoSQL databases are designed to handle large volumes of data and are optimized for specific data models. Unlike traditional relational databases, NoSQL databases do not use fixed table schemas. They are highly scalable and provide flexible data storage options. There are several types of NoSQL databases, including:

  • Document Stores: Store data in JSON-like documents. Examples include MongoDB and CouchDB.
  • Key-Value Stores: Store data as key-value pairs. Examples include Redis and DynamoDB.
  • Column Stores: Store data in columns rather than rows. Examples include Cassandra and HBase.
  • Graph Databases: Store data in graph structures with nodes, edges, and properties. Examples include Neo4j and ArangoDB.

MongoDB

MongoDB is a popular NoSQL document database that stores data in flexible, JSON-like documents. It is designed for scalability, high performance, and ease of development. Key features of MongoDB include:

  • Document Model: Data is stored in BSON (Binary JSON) format, allowing for nested documents and arrays.
  • Schema Flexibility: MongoDB does not enforce a fixed schema, allowing for dynamic and evolving data structures.
  • Indexing: Supports various types of indexes to improve query performance.
  • Aggregation Framework: Provides powerful tools for data aggregation and analysis.
  • Replication: Supports replica sets for high availability and data redundancy.
  • Sharding: Allows horizontal scaling by distributing data across multiple servers.

MongoDB is widely used in modern web applications, real-time analytics, and big data processing due to its flexibility and scalability.

MongoDB Project

This project demonstrates how to interact with a MongoDB database using various queries. The code includes examples of how to perform different types of queries, and insert a document.

Database Structure

Cluster: Cluster0
|-- Database: sample_mflix
    |-- Collections:
        |-- comments
        |-- embedded_movies
        |-- movies
        |-- sessions
        |-- theaters
        |-- users

Usage

Find the Very First Document in the Collection

# Query the collection to find the very first document
first_movie = movies_collection.find_one()
print("First Movie in the Collection:")
print(json.dumps(first_movie, indent=4, default=str))

Output:

First Movie in the Collection:
{
    "_id": "573a1390f29313caabcd42e8",
    "plot": "A group of bandits stage a brazen train hold-up, only to find a determined posse hot on their heels.",
    "genres": [
        "Short",
        "Western"
    ],
    "runtime": 11,
    "cast": [
        "A.C. Abadie",
        "Gilbert M. 'Broncho Billy' Anderson",
        "George Barnes",
        "Justus D. Barnes"
    ],
    "poster": "https://m.media-amazon.com/images/M/MV5BMTU3NjE5NzYtYTYyNS00MDVmLWIwYjgtMmYwYWIxZDYyNzU2XkEyXkFqcGdeQXVyNzQzNzQxNzI@._V1_SY1000_SX677_AL_.jpg",
    "title": "The Great Train Robbery",
    "fullplot": "Among the earliest existing films in American cinema - notable as the first film that presented a narrative story to tell - it depicts a group of cowboy outlaws who hold up a train and rob the passengers. They are then pursued by a Sheriff's posse. Several scenes have color included - all hand tinted.",
    "languages": [
        "English"
    ],
    "released": "1903-12-01 00:00:00",
    "directors": [
        "Edwin S. Porter"
    ],
    "rated": "TV-G",
    "awards": {
        "wins": 1,
        "nominations": 0,
        "text": "1 win."
    },
    "lastupdated": "2015-08-13 00:27:59.177000000",
    "year": 1903,
    "imdb": {
        "rating": 7.4,
        "votes": 9847,
        "id": 439
    },
    "countries": [
        "USA"
    ],
    "type": "movie",
    "tomatoes": {
        "viewer": {
            "rating": 3.7,
            "numReviews": 2559,
            "meter": 75
        },
        "fresh": 6,
        "critic": {
            "rating": 7.6,
            "numReviews": 6,
            "meter": 100
        },
        "rotten": 0,
        "lastUpdated": "2015-08-08 19:16:10"
    },
    "num_mflix_comments": 0
}

Find Top 5 Movies by Awards Wins

# Query the collection to find the top 5 movie titles sorted by number of wins in awards
top_movies = movies_collection.find({}, {"title": 1, "awards.wins": 1}).sort("awards.wins", -1).limit(5)
for movie in top_movies:
    print(movie)

Output:

Top 5 Movies by Number of Wins in Awards:
{
    "_id": "573a13d5f29313caabd9cae7",
    "title": "12 Years a Slave",
    "awards": {
        "wins": 267
    }
}
{
    "_id": "573a13c7f29313caabd74a4d",
    "title": "Gravity",
    "awards": {
        "wins": 231
    }
}
{
    "_id": "573a13cbf29313caabd808d2",
    "title": "Gravity",
    "awards": {
        "wins": 231
    }
}
{
    "_id": "573a13dff29313caabdb7adb",
    "title": "Birdman: Or (The Unexpected Virtue of Ignorance)",
    "awards": {
        "wins": 210
    }
}
{
    "_id": "573a13bef29313caabd5c06c",
    "title": "Boyhood",
    "awards": {
        "wins": 185
    }
}

Count the Number of Movies that Casted Clint Eastwood

# Count the number of movies that casted Clint Eastwood
clint_eastwood_movies_count = movies_collection.count_documents({"cast": "Clint Eastwood"})
print(f"Number of movies that casted Clint Eastwood: {clint_eastwood_movies_count}")

Output:

Number of movies that casted Clint Eastwood: 27

Add a New Document into the Collection with Dummy Data

# Add a new document into the collection with dummy data
new_movie = {
    "plot": "A thrilling adventure of a hero saving the world.",
    "genres": ["Action", "Adventure"],
    "runtime": 130,
    "cast": ["John Doe", "Jane Smith"],
    "poster": "https://example.com/dummy_movie_poster.jpg",
    "title": "Dummy Movie Title",
    "fullplot": "An extended plot description of the thrilling adventure.",
    "languages": ["English"],
    "released": "2023-11-01 00:00:00",
    "directors": ["John Director"],
    "rated": "PG-13",
    "awards": {
        "wins": 2,
        "nominations": 3,
        "text": "2 wins and 3 nominations."
    },
    "lastupdated": "2023-11-01 00:00:00",
    "year": 2023,
    "imdb": {
        "rating": 8.5,
        "votes": 1500,
        "id": 654321
    },
    "countries": ["USA"],
    "type": "movie",
    "tomatoes": {
        "viewer": {
            "rating": 4.5,
            "numReviews": 500,
            "meter": 90
        },
        "fresh": 10,
        "critic": {
            "rating": 8.0,
            "numReviews": 50,
            "meter": 85
        },
        "rotten": 1,
        "lastUpdated": "2023-11-01 00:00:00"
    },
    "num_mflix_comments": 10
}
result = movies_collection.insert_one(new_movie)
print(f"New movie inserted with id: {result.inserted_id}")

Output:

New movie inserted with id: 60c72b2f4f1a4e3d2c8b4567

Count the Number of Movies Released Between the Years 1990 and 1995

# Show movie titles for movies released between the years 1990 and 1995
start_year = 1990
end_year = 1995

# Count the number of movies released between the years 1990 and 1995
movies_between_years_count = movies_collection.count_documents(
    {"year": {"$gte": start_year, "$lte": end_year}}
)
print(f"Number of movies released between 1990 and 1995: {movies_between_years_count}")

Output:

Number of movies released between 1990 and 1995: 1684

Query the Top 5 Movies with IMDb Rating Greater Than 8.0

# Query the collection for the top 5 movies with an IMDb rating greater than 8.0, sorted by rating
top_high_rated_movies = movies_collection.find(
    {"imdb.rating": {"$gt": 8.0}},
    {"title": 1, "imdb.rating": 1, "_id": 0}
).sort("imdb.rating", -1).limit(5)
print("Top 5 Movies with IMDb rating greater than 8.0:")
for movie in top_high_rated_movies:
    print(movie)

Output:

Top 5 Movies with IMDb rating greater than 8.0:
{'title': 'Band of Brothers', 'imdb': {'rating': 9.6}}
{'title': 'Planet Earth', 'imdb': {'rating': 9.5}}
{'title': 'A Brave Heart: The Lizzie Velasquez Story', 'imdb': {'rating': 9.4}}
{'title': 'The Civil War', 'imdb': {'rating': 9.4}}
{'title': 'The Civil War', 'imdb': {'rating': 9.4}}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages