owned this note
owned this note
Published
Linked with GitHub
# 2a. MongoDB dokumentne NoSQL - Compass okruženje i pretraga
## ZADATAK 1. Preuzmite MongoDB Compass te ga instalirajte na vaše računalo. ZADATAK 2. Prema dokumentaciji i uputama, povežite se na svoj klaster koristeći MongoDB Compass.

## ZADATAK 3. Spojite se na neku drugu oglednu bazu podataka te kroz mongosh ispišite sve dokumente odabrane kolekcije. Dokumentirajte rješenje.
```shell
db.champions.find()
```
## ZADATAK 4. Na nekoj drugoj oglednoj bazi podataka kroz mongosh isprobajte sve operatore usporedbe navedene u gornjoj tablici. Dokumentirajte sve definirane upite i rješenja.
### $eq
```shell
db.champions.find({ name: { $eq: "Thresh" } });
```
### $gt
```shell
db.gamemodes.find( { num_players: { $gt: 1 } } );
```
### $gte
```shell
db.gamemodes.find( { num_players: { $gte: 6 } } );
```
### $in
```shell
db.listingsAndReviews.find( { amenities: { $in: ['TV'] } } );
```
### $lt
```shell
db.gamemodes.find( { num_players: { $lt: 8} } );
```
### $lte
```shell
db.gamemodes.find( { num_players: { $lte: 6} } );
```
### $ne
```shell
db.runes.find( { keystone: { $ne: 'Summon Aery' } });
```
### $nin
```shell
db.runes.find( { keystone: { $nin: ['Summon Aery', 'Electrocute'] } });
```
## ZADATAK 5. Na nekoj drugoj oglednoj bazi podataka kroz mongosh isprobajte operator AND. Dokumentirajte definirane upite i rješenja.
```shell
db.listingsAndReviews.find({ room_type: "Entire home/apt", bedrooms: { $gte: 3 }, beds: { $gte: 18 } } )
```
## ZADATAK 6. Na nekoj drugoj oglednoj bazi podataka kroz mongosh isprobajte operator OR. Dokumentirajte definirane upite i rješenja.
```shell
db.listingsAndReviews.find({ $or: [{ number_of_reviews: { $gt: 500 } }, { property_type: "House" }]});
```
## ZADATAK 7. Na nekoj drugoj oglednoj bazi podataka kroz mongosh isprobajte kombinaciju operatora AND i OR. Dokumentirajte definirane upite i rješenja.
```shell
db.listingsAndReviews.find({ room_type: "Entire home/apt", $or: [{ number_of_reviews: { $gt: 500 } }, { property_type: "House" }] })
```
## ZADATAK 8. Na nekoj drugoj oglednoj bazi podataka kroz mongosh isprobajte barem 4 upita s regularnim izrazima (od jednostavnijeg ka složenijem). Dokumentirajte definirane upite i rješenja.
### 1.
```shell
db.restaurants.find( { name: { $regex: /big.*/i } } );
```
### 2.
```shell
db.restaurants.find( { name: { $regex: /Feast/ } } );
```
### 3.
```shell
db.restaurants.find( { cuisine: { $regex: ' E.*' } } );
```
### 4.
```shell
db.runes.find({ "keystone": { $regex: /^First Strike$/, $options: 'i' } });
```
## ZADATAK 9. Na nekoj drugoj oglednoj bazi podataka kroz mongosh isprobajte operatore $exists i $type. Dokumentirajte definirane upite i rješenja.
### $exists
```shell
db.restaurants.find( { grade: { $exists: false, $nin: [ 'A', 'B' ] } } )
```
### $type
```shell
db.restaurants.find( { "name" : { $type: "string" } } )
```
## ZADATAK 10. Na nekoj drugoj oglednoj bazi podataka kroz mongosh isprobajte operator $expr. Dokumentirajte definirane upite i rješenja.
```shell
db.restaurants.find( { $expr: { $eq: [ "$borough", "Manhattan" ] } } )
```
## ZADATAK 11. Na nekoj drugoj oglednoj bazi podataka kroz mongosh isprobajte barem 4 vlastita upita nad ugniježđenim dokumentima. Dokumentirajte definirane upite i rješenja.
### 1.
```shell
db.restaurants.find( { "grades.grade": 'A' } )
```
### 2.
```shell
db.restaurants.find( { "grades.score": 12 } )
```
### 3.
```shell
db.restaurants.find( { "address.building": '469'} )
```
### 4.
```shell
db.restaurants.find( { "address.zipcode": '10019'} )
```
## ZADATAK 12. Na nekoj drugoj oglednoj bazi podataka kroz mongosh isprobajte barem 4 vlastita upita nad nizovima dokumenata. Dokumentirajte definirane upite i rješenja.
### 1.
```shell
db.posts.find( { tags: 'pair' } )
```
### 2.
```shell
db.posts.find( { "tags" : { $size: 10 } } )
```
### 3.
```shell
db.planets.find( { mainAtmosphere: 'H2' } )
```
### 4.
```shell
db.posts.find( { "tags" : { $all: ["cafe", "octave"]} } )
```
## ZADATAK 13. Na nekoj drugoj oglednoj bazi podataka kroz mongosh isprobajte barem 4 vlastita upita nad nizovima ugniježđenih dokumenata. Dokumentirajte definirane upite i rješenja.
### 1.
```shell
db.grades.find( { 'scores.score': { $lte: 70 } } )
```
### 2.
```shell
db.grades.find( { 'scores.type' : "exam" } )
```
### 3.
```shell
db.companies.find( { 'relationships.is_past': { $eq: true } } )
```
### 4.
```shell
db.companies.find( { 'products.name' : { $eq: 'Wikison Wetpaint' } } )
```
## ZADATAK 14. Na nekoj drugoj oglednoj bazi podataka kroz mongosh isprobajte barem 6 vlastitih ažuriranja dokumenata u kolekcijama. Dokumentirajte definirane upite i rješenja.
### 1.
```shell
db.champions.updateOne( {name: 'Thresh' }, {$set: {'name': 'Ahri'}} )
```
### 2.
```shell
db.champions.updateOne( {name: 'Ahri' }, {$set: {'skill': 'Low'}} )
```
### 3.
```shell
db.champions.replaceOne( {name: 'Ahri' }, {name: 'Thresh', skill: 'Very High'} )
```
### 4.
```shell
db.gamemodes.updateOne( {name: 'Blind Pick' }, {$set: {'name': 'ARAM'}} )
```
### 5.
```shell
db.gamemodes.replaceOne( {name: 'ARAM' }, {name: 'Blind Pick', num_players: 10} )
```
### 6.
```shell
db.gamemode.updateMany( { $or: [ { "_id": ObjectId("65493e292cc07e584d052cd8") }, { "_id": ObjectId("65493e6b2cc07e584d052cd9") } ] }, { $set: { "num_players": 20 } })
```
## Zadaci za vježbu
### 1. Prikažite sve dokumente u kolekciji customers.
```shell
db.customers.find()
```
### 2. Ispišite prvih pet kupaca koji imaju Platinum tier.
```shell
db.customers.aggregate([
{
$match: {
$expr: {
$gt: [
{
$size: {
$filter: {
input: { $objectToArray: "$tier_and_details" },
as: "details",
cond: { $eq: ["$$details.v.tier", "Platinum"] }
}
}
},
0
]
}
}
},
{ $limit: 5 }
]);
```
### 3. Ispišite sve transakcije napravljene u 2003. godini, a da im je cijena (price) veća od 20.
```shell
db.transactions.find({
"transactions": {
$elemMatch: {
"date": {
$gte: new ISODate("2003-01-01T00:00:00Z"),
$lt: new ISODate("2004-01-01T00:00:00Z")
},
"price": { $gt: 20 }
}
}
},
{
"transactions.$": 1
})
```
### 4. Ispišite sve transakcije kojima je ukupan iznos veći od 25000 i manji od 50000, te je kod transakcije buy. Ispis sortirajte silaznim redoslijedom.
```shell
db.transactions.aggregate([
{
$match: {
"transactions": {
$elemMatch: {
"transaction_code": "buy",
"total": {
$gt: "25000", // These are strings to match the stored data type
$lt: "50000"
}
}
}
}
},
{
$unwind: "$transactions"
},
{
$match: {
"transactions.transaction_code": "buy",
"transactions.total": {
$gt: "25000", // Again, these are strings
$lt: "50000"
}
}
},
{
$sort: { "transactions.total": -1 }
}
]);
```
### 5. Ispišite id kupca, njegovo ime, korisničko ime i detalje o sloju (tiers and details), za one kupce kojima su zadnja tri slova korisničkog imena 'ler'.
```shell
db.customers.aggregate([
{
$match: {
username: { $regex: /ler$/ }
}
},
{
$project: {
_id: 1,
name: 1,
username: 1,
tier_and_details: 1
}
}
])
```
### 6. Ispišite id kupca, njegovo ime, adresu i email, za one kupce koji negdje u svojem imenu sadrže niz znakova 'li'.
```shell
db.customers.aggregate([
{
$match: {
name: { $regex: /li/ }
}
},
{
$project: {
_id: 1,
name: 1,
address: 1,
email: 1
}
}
])
```
### 7. Ispišite račune kojima je limit manji ili jednak 10000 i sadrže proizvode koji su ili InvestmentStock ili Commodity.
```shell
db.accounts.find({
$and: [
{ limit: { $lte: 10000 } },
{ products: { $in: ["InvestmentStock", "Commodity"] } }
]
})
```
### 8. Ispišite ime kupca, adresu i datum rođenja za sve kupce koji nemaju račune 371138 i 422649, ili im email počinje slovom 'c'.
```shell
db.customers.find({
$nor: [
{ accounts: 371138 },
{ accounts: 422649 }
],
email: /^c/
}, {
name: 1,
address: 1,
birthdate: 1
})
```
### 9. Ispišite sve podatke o kupcima kod kojih je drugi element benefits niza 'concierge services'.
```shell
db.customers.aggregate([
{
$addFields: {
tier_and_details_array: {
$objectToArray: "$tier_and_details"
}
}
},
{ $unwind: "$tier_and_details_array" },
{ $unwind: "$tier_and_details_array.v.benefits" },
{
$match: {
"tier_and_details_array.v.benefits": "concierge services",
"tier_and_details_array.v.active": true
}
},
{
$group: {
_id: "$_id",
username: { $first: "$username" },
name: { $first: "$name" },
address: { $first: "$address" },
birthdate: { $first: "$birthdate" },
email: { $first: "$email" },
accounts: { $first: "$accounts" },
tier_and_details: { $push: "$tier_and_details_array.v" }
}
}
]);
```
### 10. Ispišite id kupca, njegovo ime i detalje o sloju (tier and details), za one kupce koji imaju aktivan (true) srebrni i zlatni sloj.
```shell
db.customers.find({
$and: [
{ 'tier_and_details.Silver.active': true },
{ 'tier_and_details.Gold.active': true }
]
});
```