Академический Документы
Профессиональный Документы
Культура Документы
DOCUMENTOS
Agregado 1
◻ Domain-Driven Design
redundância
Agregado 2
redundância
redundância
Modelo agregado – versão 1
6
Agregado 1
◻ Domain-Driven Design
Agregado 2
...
Documento
8
◻ Int
◻ Double
◻ String
◻ Array
◻ ObjectId
◻ Boolean
◻ Date
ObjectId
12
var mydoc = {
_id: ObjectId("5099803df3f4948bd2f98391"),
name: { first: "Alan", last: "Turing" },
birth: new Date('Jun 23, 1912'),
death: new Date('Jun 07, 1954'),
contribs: [ "Turing machine", "Turing test", "Turingery" ],
views : NumberLong(1250000)
}
_id é reservado
Não pode ter $ ou . ou null
Acesso a vetores
15
◻ “<array>.<index>”
{
...
contribs: [ "Turing machine", "Turing test", "Turingery" ],
...
}
Acesso a documentos aninhados
16
◻ “<embedded document>.<field>”
Acesso ao sobrenome
■ “name.last”
Acesso ao número do telefone
■ "contact.phone.number"
{
...
name: { first: "Alan", last: "Turing" },
contact: { phone: { type: "cell", number: "111-222-3333" } },
...
}
Selecionando / criando uma base
17
base
Coleção
1
Coleção
2
Coleção de documentos
18
◻ Exemplo
db.myNewCollection1.insertOne( { x: 1 } )
Operações sobre bases
21
◻ db
Apresenta a base em uso
◻ show dbs
Lista as bases disponíveis
Operações sobre coleções
22
◻ db.createCollection(name, {option1,...,option_n})
db.createCollection(<name>, { capped: <boolean>,
autoIndexId: <boolean>,
size: <number>,
max: <number>,
storageEngine: <document>,
validator: <document>,
validationLevel: <string>,
◻ db.getCollectionInfos() validationAction: <string>,
indexOptionDefaults: <document>,
viewOn: <string>,
pipeline: <pipeline>,
collation: <document> } )
23
Inserção
Inserção de dados
24
db.inventory.insertOne(
{ item: "canvas", qty: 100, tags: ["cotton"],
size: { h: 28, w: 35.5, uom: "cm" } }
)
documento
resposta
Sucesso
Identificador do documento
da operação
criado
InsertOne especificando o _id
26
◻ Retorno
db.universidade.insertOne( {
disciplinas: [
{ nome: “algoritmos”, sala: 72, turma: “E”},
{ nome: “estrutura de dados", sala: 74, turma: “F”}
]
})
InsertMany
28
db.inventory.insertMany(
[
{ item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
{ item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
{ item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom:
"cm" } }
])
29
Consulta
Exemplo produtos em estoque
30
db.inventory.insertMany([
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);
Seleciona documentos
31
SQL correspondente: SELECT * FROM inventory WHERE status = "A" AND qty < 30
Condição OR
35
SQL correspondente: SELECT * FROM inventory WHERE status = "A" OR qty < 30
Combinando AND e OR
36
SQL correspondente:
SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")
Ordenação e limite de resultado
37
db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] },
{ item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] },
{ item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] },
{ item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] },
{ item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }
]);
Procura um elemento no vetor
47
db.inventory.find( {
"instock": { $elemMatch: { qty: 5, warehouse: "A" } }
})
Consulta em vetor de documentos
58
db.inventory.find({
"instock": { $elemMatch: { qty: { $gt: 10, $lte: 20 } } }
})
Seleção de campos de retorno
59
db.inventory.insertMany( [
{ item: "journal", status: "A", size: { h: 14, w: 21, uom: "cm" }},
{ item: "notebook", status: "A", size: { h: 8.5, w: 11, uom: "in" }},
{ item: "paper", status: "D", size: { h: 8.5, w: 11, uom: "in" }},
{ item: "planner", status: "D", size: { h: 22.85, w: 30, uom: "cm" }},
{ item: "postcard", status: "A", size: { h: 10, w: 15.25, uom: "cm" }}
]);
Seleção de campos de retorno
60
◻ db.inventory.find({<consulta>}, {campos_retorno})
◻ db.inventory.find({<consulta>}, {campos_retorno})
SQL correspondente: SELECT _id, item, status from inventory WHERE status = "A"
Seleção de campos de retorno
62
db.inventory.find(
{ status: "A" }, { item: 1, status: 1, _id: 0 } )
SQL correspondente: SELECT item, status from inventory WHERE status = "A"
Seleção de campos de retorno
63
Atualização
Atualização de documentos
67
◻ Dados de entrada
db.inventory.insertMany( [
{ item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" },
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "cm" }, status: "A" },
{ item: "mousepad", qty: 25, size: { h: 19, w: 22.85, uom: "cm" }, status: "P" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },
{ item: "sketchbook", qty: 80, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "sketch pad", qty: 95, size: { h: 22.85, w: 30.5, uom: "cm" }, status: "A" }
] );
Atualiza um documento
68
◻ db.collection.updateOne()
db.collection.updateOne(
{<condição>},
{
<update operator>: { <field1>: <value1>, ... },
<update operator>: { <field2>: <value2>, ... },
...
}
)
Atualiza um documento
69
db.inventory.updateOne(
{ item: "paper" }, Cria o campo se ele não existe
{
$set: { "size.uom": "cm", status: "P" },
$currentDate: { lastModified: true }
})
Resultado:
Exemplo sistema da universidade
70
◻ db.collection.updateMany()
db.inventory.updateMany(
{ "qty": { $lt: 50 } },
{
$set: { "size.uom": "in", status: "P" },
$currentDate: { lastModified: true }
}
)
Substitui um documento
73
◻ db.collection.replaceOne()
db.inventory.replaceOne(
{ item: "paper" },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, {
warehouse: "B", qty: 40 } ] }
Novos dados do documento
)
74
Remoção
Remoção de dados
75
1. https://docs.mongodb.com