Skip to content

Instantly share code, notes, and snippets.

@fmasanori
Last active August 20, 2025 23:33
Show Gist options
  • Save fmasanori/5923215 to your computer and use it in GitHub Desktop.
Save fmasanori/5923215 to your computer and use it in GitHub Desktop.
Oficina MongoDB e Python FISL
Oficina MongoDB e Python - FISL
Install python 3.x (provavelmente vc já tem, 3.7 no mínimo)
https://www.python.org/downloads/
para testar python -V
Install mongoDB última versão
https://www.mongodb.org/downloads
criar diretório \data\db com as permissões necessárias para testar bin\mongod (servidor)
pip install pymongo
pip install bottle
Visão geral MongoDB e NoSQL:
Não relacional -> JSON database (BSON) -> dev like data
Schemaless -> flexível -> produtividade
No joins, no transactions
Equilíbrio: escalabilidade e funcionalidade
Python Básico:
dir e help
tarefas: execute os comandos:
a = 'abacate'
b = 42
a, b = b, a
listas
dicionários
tarefas:
monte um dicionário quitanda com chave 'frutas' e 'pera', 'abacate', 'banana' como valores associados
monte um dicionário end com chave 'address' que possui outro dicionário como valor, com as chaves 'street_address',
'city', 'zipcode' com os valores respectivos: 'Puc RS', 'PoA', 42
monte um dicionário cadastro com chaves 'nome' = seu nome, 'emails' = lista de seus emails
urllib e json
https://gist.github.com/fmasanori/5901473 (chuck norris)
https://gist.github.com/fmasanori/4126108 (reddit)
Mongo shell (JavaScript)
help
show dbs
show collections
db.help()
db.test.help()
post = {"title": "My Blog Post",
"content": "Here's my blog post.",
"date": new Date()}
db.blog.insertOne(post)
db.blog.find()
db.blog.find().pretty()
db.blog.findOne()
db.blog.updateOne({title: "My Blog Post"}, {$set: {"comments":[]}})
db.blog.findOne()
db.blog.deleteOne({title: "My Blog Post"})
db.blog.findOne()
CRUD python
Create = Insert, Read = Find, Update = Update, Delete = Remove
https://gist.github.com/fmasanori/5903005 (fisl 05)
quais as diferenças com o anterior?
como tratar exceções?
https://gist.github.com/fmasanori/5922895 (fisl 05 exception)
tarefas:
inserir na collection names {"name": "seunome"}
ler a collection names e mostrar o primeiro registro (fisl 06)
vamos fazer a versão web agora...
https://gist.github.com/fmasanori/5903268 (fisl 07 - bottle)
mongo shell > db.names.remove({name: "seunome"})
mongo shell > db.names.save({name: "Mongo is great"})
dar refresh no browser
Web Python com Bottle
https://gist.github.com/fmasanori/5922929 (fisl 08)
https://gist.github.com/fmasanori/5922942 (fisl 09)
https://gist.github.com/fmasanori/5922955 (hello_world.tpl)
https://gist.github.com/fmasanori/5922968 (fisl 10)
https://gist.github.com/fmasanori/5922989 (hello_world2.tpl)
https://gist.github.com/fmasanori/5922999 (fruit_selection.tpl)
https://gist.github.com/fmasanori/5923012 (fisl 11)
Tarefa para casa:
montar uma collection Facebook
nomes e ids dos amigos
app que sorteia um amigo e mostra sua foto de perfil recente
import random > random.choice(lista_amigos)
faça um template apropriado...
https://gist.github.com/fmasanori/5923032 - Create Student
Collection
https://gist.github.com/fmasanori/5923106 (fisl 12)
https://gist.github.com/fmasanori/5923121 (fisl 13)
https://gist.github.com/fmasanori/5923130 (fisl 14)
https://gist.github.com/fmasanori/5923142 (fisl 15)
https://gist.github.com/fmasanori/5923160 (fisl 16)
https://gist.github.com/fmasanori/5923175 (fisl 17)
https://gist.github.com/fmasanori/5923187 (fisl 18)
https://gist.github.com/fmasanori/5923199 (fisl 19)
Blog in relational model:
authors:
author_id,
name,
email,
password
posts:
post_id,
author_id
title,
body,
publication_date
comments:
comment_id,
name,
email,
comment_text
post_comments:
post_id,
comment_id
tags
tag_id
name
post_tags
post_id
tag_id
Quantas tabelas são necessárias para o blog? Seis!
Blog in document model:
Post collection
Quantas collections são necessárias para o blog? Uma!
Você acessa uma tag sozinha? Não = Embed
Difícil mudar as tags[biking] por tags[cycling]
Você acessa um comentário sozinho? Não = Embed
Muito raro ter que acessar tags ou comentários
independentemente
Como você irá acessar os seus dados? Modelagem começa aqui.
Schema Design == To Embed OR Not To Embed (that is the
Question)
Schema Design Emily se der tempo...
Indexes
Aggregation Framework
Replication
Sharding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment