MongoDB

MongoDB – dokumendipõhine andmebaasihaldussüsteem, mis ei vaja tabeli skeemi kirjeldust.

Praktiline osa

Loome andmebaas

Lisame andmed (insert)

Vaatame kõik mis on kollektsioonis (find)

Uuendame andmed (update)

Kustutamine (delete)

Teeme 2 tegevuse (BulkWrite)

MongoDB ühendamine

Veebileht (kasutasin nodejs + Express)

const express = require("express");
const { MongoClient, ObjectId } = require("mongodb");

const app = express();
const port = 3000;

const client = new MongoClient("mongodb+srv://admin:123@cluster0.zdi1yox.mongodb.net/Masha?retryWrites=true&w=majority&appName=Cluster0");

app.use(express.urlencoded({ extended: true }));

app.get("/", async (req, res) => {
    try {
        await client.connect();
        const db = client.db("Masha");
        const users = db.collection("users");

        const allUsers = await users.find().toArray();

        let html = `
        <html>
        <head>
            <title>Users</title>
            <style>
                body {
                    font-family: Arial, sans-serif;
                    background: #f4f4f9;
                    margin: 0;
                    padding: 20px;
                }
                h1 {
                    text-align: center;
                    color: #333;
                }
                ul {
                    list-style: none;
                    padding: 0;
                    max-width: 500px;
                    margin: 20px auto;
                }
                li {
                    background: #fff;
                    margin: 10px 0;
                    padding: 12px 18px;
                    border-radius: 8px;
                    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
                    font-size: 16px;
                    color: #444;
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                }
                button {
                    background: #e74c3c;
                    color: #fff;
                    border: none;
                    padding: 8px 12px;
                    border-radius: 6px;
                    cursor: pointer;
                    font-size: 14px;
                }
                button:hover {
                    background: #c0392b;
                }
            </style>
        </head>
        <body>
            <h1>Mangodb nimekiri</h1>
            <ul>`;

        allUsers.forEach(u => {
            html += `
                <li>
                    <span><b>${u.name}</b> — ${u.age}</span>
                    <form action="/delete/${u._id}" method="POST" style="margin:0;">
                        <button type="submit">kustutama</button>
                    </form>
                </li>`;
        });

        html += `</ul>
        </body>
        </html>`;

        res.send(html);
    } catch (e) {
        console.error("Error:", e);
        res.status(500).send("Server error");
    }
});


app.post("/delete/:id", async (req, res) => {
    try {
        await client.connect();
        const db = client.db("Masha");
        const users = db.collection("users");

        await users.deleteOne({ _id: new ObjectId(req.params.id) });

        res.redirect("/");
    } catch (e) {
        console.error("Error:", e);
        res.status(500).send("Delete error");
    }
});

app.listen(port, () => {
    console.log(`Server running at http://localhost:${port}`);
});

Zone

Minu töö zone’is

Ühenduse loomiseks kasutasin SSH-d ja selle kasutamiseks lõin võtme ning lisasin selle zone’i