Skip to content
Araz Shah
Menu
  • Home
  • About me
  • Contact me
  • CV
  • Online Courses
    • Apply Now !
    • In-Depth
    • Courses
      • Concepts
      • Python Course
      • GIS Developer Course
    • Price
Menu

user login checks using MongoDB and Python

Posted on August 18, 2023 by admin

To perform user login checks using MongoDB and Python, you can follow these steps:

Step 1: Set Up MongoDB
Make sure you have MongoDB installed and running. You can follow the steps mentioned earlier to set up a MongoDB container using Docker.

Step 2: Install PyMongo
Install the PyMongo library if you haven’t already:

pip install pymongo

Step 3: Create User Collection in MongoDB
Assuming you want to store user information in a collection named “users,” create the collection in your MongoDB database. You can do this using a MongoDB client like the MongoDB Compass GUI or by running MongoDB shell commands.

Step 4: User Login Check Script
Here’s an example Python script that demonstrates how to check user login using PyMongo:

from pymongo import MongoClient
from bson.objectid import ObjectId  # To handle MongoDB ObjectIds
from hashlib import sha256  # For password hashing

# Create a MongoDB client
client = MongoClient('localhost', 27017)

# Access the desired database and collection
db = client['mydatabase']
users_collection = db['users']

def login(username, password):
    # Hash the password for comparison
    hashed_password = sha256(password.encode()).hexdigest()

    # Search for the user by username and hashed password
    user = users_collection.find_one({'username': username, 'password': hashed_password})

    return user

# User input for login
username_input = input("Enter your username: ")
password_input = input("Enter your password: ")

# Perform login check
user = login(username_input, password_input)

if user:
    print("Login successful!")
    print("User details:", user)
else:
    print("Invalid username or password")

# Close the client connection
client.close()

In this example, replace 'mydatabase' with your actual database name, and make sure you have a 'users' collection with documents that contain 'username' and 'password' fields (where the password is hashed, preferably with a strong hashing algorithm).

Remember that this is a basic example. In a real-world application, you should consider using stronger security measures like salting and using more advanced hashing algorithms, as well as handling exceptions and validation properly.

Step 5: Clean Up
When you’re done, you can stop and remove the MongoDB container as described earlier.

Category: python, Tutorials

Post navigation

← Configuring Docker with MongoDB and connecting it with PyMongo
write tests using pytest for a MongoDB →

Recent Posts

  • Geospatial Risk Assessment: A Python Approach
  • Analyzing Employee Arrival Patterns and Delays Using Geospatial Data
  • Real-Time GPS Tracking on a Web Map using FastAPI & Leaflet
  • How to Create a Simple WebGIS with FastAPI, PostGIS, and Leaflet.js
  • Graph Coloring: How Many Colors Do You Need?

Archives

  • May 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • September 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • April 2023

Categories

  • Courses
  • Events
  • GIS
  • Linux
  • News
  • programming
  • python
  • Tutorials
  • Videos
  • May 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • September 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • April 2023
  • Courses
  • Events
  • GIS
  • Linux
  • News
  • programming
  • python
  • Tutorials
  • Videos

Araz Shahkarami

I’m a software enthusiast with a deep love for crafting robust and efficient solutions. My journey into the world of programming began several years ago when I was introduced to the world of code. Since then, I’ve been on an exhilarating ride of learning, problem-solving, and continuous improvement.

© 2025 Araz Shah | Powered by Minimalist Blog WordPress Theme