Skip to content

Instantly share code, notes, and snippets.

@pedrovasconcellos
Created April 11, 2025 00:01
Show Gist options
  • Save pedrovasconcellos/90c8f3e3978fc3950b9ae7bf0a4dd108 to your computer and use it in GitHub Desktop.
Save pedrovasconcellos/90c8f3e3978fc3950b9ae7bf0a4dd108 to your computer and use it in GitHub Desktop.
Insert random data into MongoDB
from pymongo import MongoClient
from datetime import datetime
import uuid
from urllib.parse import quote_plus
USER = "master"
PASSWORD = quote_plus("password") # encodes the password
HOST = "localhost"
AUTH_DB = "admin"
DATABASE = "database_name"
# builds the URI with escaped password
SOURCE_URI = f"mongodb://{USER}:{PASSWORD}@{HOST}:27017/?authSource={AUTH_DB}"
# 🧠 Connection
client = MongoClient(SOURCE_URI)
db = client[DATABASE]
# 🔄 Drop collections to recreate them clean
db.people.drop()
db.addresses.drop()
db.person_games.drop()
# 👤 People
people = [
{
"_id": str(uuid.uuid4()),
"name": "João Silva",
"age": 30,
"email": "[email protected]",
"height_m": 1.75,
"birth_date": datetime(1993, 4, 10),
"active": True,
"documents": {"cpf": "123.456.789-00", "rg": "12.345.678-9"},
"hobbies": ["soccer", "cooking", "video games"]
},
{
"_id": str(uuid.uuid4()),
"name": "Maria Oliveira",
"age": 25,
"email": "[email protected]",
"height_m": 1.62,
"birth_date": datetime(1998, 12, 5),
"active": False,
"documents": {"cpf": "987.654.321-00", "rg": "98.765.432-1"},
"hobbies": ["reading", "yoga", "cinema"]
},
{
"_id": str(uuid.uuid4()),
"name": "Carlos Souza",
"age": 40,
"email": "[email protected]",
"height_m": 1.80,
"birth_date": datetime(1984, 1, 20),
"active": True,
"documents": {"cpf": "456.123.789-00", "rg": "45.612.378-9"},
"hobbies": ["walking", "chess"]
}
]
# 🏠 Addresses
addresses = [
{
"person_id": people[0]["_id"],
"street": "Rua das Flores",
"number": 123,
"city": "São Paulo",
"state": "SP",
"zip_code": "01010-000",
"last_updated": datetime.now(),
"location": {"latitude": -23.55052, "longitude": -46.633308},
"landmarks": ["Sé Square", "Sé Subway Station"]
},
{
"person_id": people[1]["_id"],
"street": "Av. Brasil",
"number": 456,
"city": "Rio de Janeiro",
"state": "RJ",
"zip_code": "20040-002",
"last_updated": datetime.now(),
"location": {"latitude": -22.906847, "longitude": -43.172896},
"landmarks": ["Christ the Redeemer", "Copacabana Beach"]
},
{
"person_id": people[2]["_id"],
"street": "Rua Central",
"number": 789,
"city": "Belo Horizonte",
"state": "MG",
"zip_code": "30130-010",
"last_updated": datetime.now(),
"location": {"latitude": -19.916681, "longitude": -43.934493},
"landmarks": ["Liberty Square", "Arts Palace"]
}
]
# 🎮 Games
games = [
{
"person_id": people[0]["_id"],
"game": "FIFA 23",
"platform": "PS5",
"genre": "Sports",
"hours_played": 120.5,
"rating": 8.2,
"last_played_at": datetime(2024, 12, 15, 19, 30),
"settings": {"language": "Portuguese", "resolution": "4K"},
"achievements": ["World Championship", "Top Scorer"]
},
{
"person_id": people[0]["_id"],
"game": "Elden Ring",
"platform": "PC",
"genre": "RPG",
"hours_played": 200.0,
"rating": 9.7,
"last_played_at": datetime(2025, 1, 2, 22, 10),
"settings": {"language": "English", "resolution": "1440p"},
"achievements": ["Defeated Malenia", "No-death playthrough"]
},
{
"person_id": people[1]["_id"],
"game": "The Sims 4",
"platform": "PC",
"genre": "Simulation",
"hours_played": 85.3,
"rating": 7.5,
"last_played_at": datetime(2025, 3, 10, 16, 45),
"settings": {"language": "Portuguese", "resolution": "1080p"},
"achievements": ["Dream House", "Perfect Life"]
},
{
"person_id": people[2]["_id"],
"game": "God of War",
"platform": "PS4",
"genre": "Action",
"hours_played": 95.7,
"rating": 9.1,
"last_played_at": datetime(2025, 2, 8, 20, 0),
"settings": {"language": "Portuguese", "resolution": "4K"},
"achievements": ["Defeated Baldur", "Unlocked the Serpent"]
}
]
# 📥 Insertion
db.people.insert_many(people)
db.addresses.insert_many(addresses)
db.person_games.insert_many(games)
# 🔧 Indexes
db.people.create_index("email")
db.people.create_index("age")
db.addresses.create_index("city")
db.addresses.create_index("zip_code")
db.person_games.create_index("person_id")
db.person_games.create_index("game")
print("✅ Data and indexes created successfully!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment