Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skatesham/66cb6d45054e25fc089359ddcd49a843 to your computer and use it in GitHub Desktop.
Save skatesham/66cb6d45054e25fc089359ddcd49a843 to your computer and use it in GitHub Desktop.
Oficina MongoDB e Python FISL
Oficina MongoDB e Python - FISL
Resumo university.mongodb.com/courses/M101P/about
Install python 2.7 (provavelmente vc já tem)
https://www.python.org/downloads/
para testar python -V
Install mongoDB 3.4
https://www.mongodb.org/downloads
criar diretório \data\db com as permissões necessárias para testar bin\mongod (servidor)
bin\mongo (mongo shell)
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
http://www.slideshare.net/fmasanori/mongodb-e-python
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.insert(post)
db.blog.find()
db.blog.find().pretty()
db.blog.findOne()
post.comments = []
db.blog.update({title: "My Blog Post"}, post)
db.blog.findOne()
db.blog.remove({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
Referências:
MongoDB 2a edição:
http://shop.oreilly.com/product/0636920028031.do
Tutorial Pycon2012:
http://pyvideo.org/video/914/mongodb-and-python
https://github.com/behackett/presentations/tree/master/pycon_2012
Schema Design:
http://www.10gen.com/presentations/schema-design-3
Education 10gen:
https://education.10gen.com/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment