Untitled
unknown
plain_text
a year ago
6.7 kB
5
Indexable
Zadanie 1 - Mongo Shell
1) Utwórz bazę danych o nazwie university i kolekcję students.
use university
db.createCollection("students")
2) Wstaw 10 studentów do kolekcji students z polami: firstName, lastName, age, group,
yearOfStudy.
Informacje do pól age, group, yearOfStudy:
a) Dla przynajmniej jednego studenta wpisz Math w liście przedmiotów (subjects)
b) age podawaj w granicach 16-25 lat.
c) group podawaj wpisz A, B, C, D lub E.
d) yearOfStudy wpisz wartości liczbowe
db.students.insertMany([
{
firstName: "Hubert",
lastName: "Rozumek",
age: 20,
group: "A",
yearOfStudy: 2,
subjects: ["Math", "Physics"]
},
{
firstName: "Jan",
lastName: "Kowalski",
age: 22,
group: "B",
yearOfStudy: 3,
subjects: ["Biology", "Chemistry"]
},
{
firstName: "Marek",
lastName: "Ogarek",
age: 19,
group: "C",
yearOfStudy: 1,
subjects: ["Math", "English"]
},
{
firstName: "Robert",
lastName: "Nowak",
age: 23,
group: "D",
yearOfStudy: 4,
subjects: ["History", "Geography"]
},
{
firstName: "Filip",
lastName: "Kowalski",
age: 18,
group: "E",
yearOfStudy: 1,
subjects: ["Math", "Art"]
},
{
firstName: "Hubert",
lastName: "Ogórek",
age: 21,
group: "A",
yearOfStudy: 3,
subjects: ["Economics", "Math"]
},
{
firstName: "Hubert",
lastName: "Marchewka",
age: 25,
group: "B",
yearOfStudy: 5,
subjects: ["Math", "Computer Science"]
},
{
firstName: "Hubert",
lastName: "Rzodkiewka",
age: 24,
group: "C",
yearOfStudy: 4,
subjects: ["Math", "Philosophy"]
},
{
firstName: "Hubert",
lastName: "Pietruszka",
age: 17,
group: "D",
yearOfStudy: 1,
subjects: ["Chemistry", "Math"]
},
{
firstName: "Hubert",
lastName: "Roszponka",
age: 16,
group: "E",
yearOfStudy: 1,
subjects: ["Physics", "Math"]
}
]);
3) Dodaj 5 dokumentów (grupowo) do kolekcji students
db.students.insertMany([
{
firstName: "Oliver",
lastName: "Kowalski",
age: 18,
group: "A",
yearOfStudy: 1,
subjects: ["Math", "Chemistry"]
},
{
firstName: "Sonia",
lastName: "Nowak",
age: 19,
group: "B",
yearOfStudy: 2,
subjects: ["Physics", "Math"]
},
{
firstName: "Liam",
lastName: "Kozlowski",
age: 20,
group: "C",
yearOfStudy: 2,
subjects: ["Biology", "Math"]
},
{
firstName: "Emma",
lastName: "Stone",
age: 21,
group: "D",
yearOfStudy: 3,
subjects: ["Math", "Literature"]
},
{
firstName: "Leonardo",
lastName: "Di Caprio",
age: 22,
group: "E",
yearOfStudy: 3,
subjects: ["History", "Math"]
}
]);
4) Zaktualizuj pole yearOfStudy dla wszystkich studentów, których age jest większe niż 20
lat.
db.students.updateMany(
{age: {$gt: 20}},
{$set: {yearOfStudy: 4}}
)
5) Usuń wszystkich studentów, których age jest mniejsze niż 18 lat oraz tych, których age
jest większy niż 22 lata.
db.students.deleteMany(
{age: {$lt: 22}}
)
6) Dodaj 2 dokumenty do kolekcji students (pojedyńczo).
db.students.insertOne({firstName: "Tomek", lastName: "Lewandowski", age: 23, group: "D", yearOfStudy: 3, subjects: ["History", "Math"]})
db.students.insertOne({firstName: "Piotr", lastName: "Oregano", age: 20, group: "E", yearOfStudy: 2, subjects: ["History", "Math"]})
7) Wyszukaj studentów, którzy są na trzecim roku studiów (yearOfStudy = 3).
db.students.find({ yearOfStudy: 3 }).pretty();
Zadanie 2 - Replica Sets
Przedstaw wszystkie komendy potrzebne do wykonania poniższych zadań.
1) Utwórz strukturę katalogów dla trzech instancji: jedna primary i dwie secondary (porty
10000, 20000, 30000).
mkdir -p /data/rs1 /data/rs2 /data/rs3
mongod --replSet rs0 --port 10000 --dbpath /data/rs1 --oplogSize 128 --fork --logpath /data/rs1/mongod.log
mongod --replSet rs0 --port 20000 --dbpath /data/rs2 --oplogSize 128 --fork --logpath /data/rs2/mongod.log
mongod --replSet rs0 --port 30000 --dbpath /data/rs3 --oplogSize 128 --fork --logpath /data/rs3/mongod.log
2) Skonfiguruj replica set.
mongo --port 10000
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "localhost:10000" },
{ _id: 1, host: "localhost:20000" },
{ _id: 2, host: "localhost:30000" }
]
})
3) Dodaj nową instancję primary (port 40000) i usuń instancję na porcie 30000.
mkdir -p /data/rs4
mongod --replSet rs0 --port 40000 --dbpath /data/rs4 --oplogSize 128 --fork --logpath /data/rs4/mongod.log
mongo --port 10000
# Dodaj
rs.add("localhost:40000")
# Usuń
rs.remove("localhost:30000")
4) Zrekonfiguruj replica set.
cfg = rs.conf()
cfg.members[0].priority = 1
cfg.members[1].priority = 0.5
cfg.members[2].priority = 0.5
cfg.members[3].priority = 2
rs.reconfig(cfg)
5) Wyłącz instancję primary i sprawdź, która secondary stanie się nowym primary.
kill <PID>
mongo --port 20000 # lub 40000, jeśli port 20000 nie odpowiada
rs.status()
6) Wstaw dane studenta takie jak w zadaniu 1.
mongo --port <new_primary_port>
db.students.insertMany([
{
firstName: "Karol",
lastName: "Nowak",
age: 20,
group: "A",
yearOfStudy: 2,
subjects: ["Math", "Physics"]
},
{
firstName: "Will",
lastName: "Smith",
age: 22,
group: "B",
yearOfStudy: 3,
subjects: ["Biology", "Chemistry"]
},
{
firstName: "Bill",
lastName: "Gates",
age: 19,
group: "C",
yearOfStudy: 1,
subjects: ["Math", "English"]
},
{
firstName: "Robert",
lastName: "Brown",
age: 23,
group: "D",
yearOfStudy: 4,
subjects: ["History", "Geography"]
},
{
firstName: "Paul",
lastName: "McCartney",
age: 18,
group: "E",
yearOfStudy: 1,
subjects: ["Math", "Art"]
}
]);
7) Odczytaj dane z instancji secondary.
mongo --port 20000 # Połącz z instancją secondary
rs.slaveOk()
db.students.find().pretty();
Editor is loading...
Leave a Comment