MongoDB

Collection commands

  • find
  • insert
  • insertMany

[Find] conditions

$or

db.<collection-name>.find(
  {
    $or: [..., ..., ... ...]
  }
);

$lt, $gt, $lte, $gte

db.<collection-name>.find(
  {
    <key>: {
      <$lt | $gt | $lte | $gte>: <number>
    }
  }
);

Andmebaas ja collection loomine

Andmebaas kustutamine

Dokumendid lisamine

Dokumendid otsimine

Kõik dokumendid

Tingimusega

“Või”

“Väikem kui”

“Suurem kui”

Sorteerimine

Uuendamine

Kustutamine

Suurem kui 1 operatsioon jaoks

Laiendatud otsing

Node.js & MongoDB

MongoDB alla laadimine

MongoDB + Node.js

Backend

Main server file ( index.js )

import { MongoClient, ObjectId } from "mongodb";
import express from "express"
import cors from "cors"

const app = express();
app.use(express.static("public"))
app.use(express.json())
app.use(cors())
const PORT = 3000;


const client = new MongoClient(CONNECTION)

const defaultData = [
    {
        "nimi": "david",
        "vanus": 18
    },
    {
        "nimi": "martin",
        "vanus": 18
    },
    {
        "nimi": "vlad",
        "vanus": 19
    },
    {
        "nimi": "gleb",
        "vanus": 18
    }
]

const resetData = async () => {
    try {
        client.connect()
        console.log("has connected")
        const collection = await client.db().collection("users")
        await collection.deleteMany({});
        await collection.insertMany(defaultData);
    }
    catch(e){
        console.log(e)
    }
};

app.get("/data", async (req, res)=> {

    client.connect()
    const usersCollection = await client.db().collection("users")
    const users = await usersCollection.find()
    res.status(200).json(await users.toArray())
})
app.delete("/remove/:id", async (req, res)=> {
    const { id } = req.params;
    client.connect()
    const usersCollection = await client.db().collection("users")
    const resqq = await usersCollection.deleteOne({
        _id: new ObjectId(id)
    })
    console.log(resqq)
    res.status(200);
})

app.listen(PORT, ()=> {
    console.log("server is listening")
})

resetData();

HTML + CSS + JS

HTML file ( public/index.html )

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MongoDB</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="container">
        <header style="text-align: center;">
            <h1>MongoDB collection</h1>
        </header>
        <div class="center">

            <table>
                <thead>
                    <th>ID</th>
                    <th>Nimi</th>
                    <th>Vanus</th>
                </thead>
                <tbody id="tbody">
                    
                </tbody>
            </table>
        </div>
        <footer style="width:100%; height: 150px; background-color: #000; color:#fff; text-align: center; justify-content: center; display: flex; align-items: center;">
            by phenibut645
        </footer>
    </div>
    <script src="script.js"></script>
</body>
</html>

CSS file ( public/styles.css )

body {
    padding: 0;
    margin: 0;
    font-family: Arial, Helvetica, sans-serif;
}

.container {
    display:flex;
    flex-direction: column;
    min-height: 100vh;
}

table, th, td {
    padding: 5px;
    text-align: center;
    border: 1px solid black;
    border-collapse: collapse;
    color: #fff;
}
th{
    background-color: rgb(46, 0, 99);
}
td{
    background-color: #19182c;
}

.center{
    flex:1;
    width: 100%;
    display:flex;
    justify-content: center;
    align-items: center;
}

JS File ( public/script.js )

const tbody = document.getElementById("tbody")

async function getData(){
    const data = await fetch("http://localhost:3000/data");
    const json = await data.json()
    tbody.innerHTML = ""
    json.forEach(el => {
        const row = document.createElement("tr")
        const idElement = document.createElement("td")
        idElement.innerText = el["_id"]
        row.appendChild(idElement)

        const nameElement = document.createElement("td")
        nameElement.innerText = el["nimi"]
        row.append(nameElement)

        const ageElement = document.createElement("td")
        ageElement.innerText = el["vanus"]
        row.append(ageElement)

        const removeElement = document.createElement("td")
        removeElement.addEventListener("click", async (_)=> {
            await fetch(`http://localhost:3000/remove/${el["_id"]}`, {
                method:"DELETE"
            })
            await getData();
        })
        row.append(removeElement)
        tbody.appendChild(row)
    })

    console.log(json)
}

getData()