Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MongoDB für Anfänger

Avatar for alcaeus alcaeus
September 12, 2025

MongoDB für Anfänger

Work in progress talk about MongoDB at Check24, 2025-09-12

Avatar for alcaeus

alcaeus

September 12, 2025
Tweet

More Decks by alcaeus

Other Decks in Programming

Transcript

  1. Developer Data Platform MongoDB Atlas • Funktionsumf a ng entspricht

    Enterprise Adv a nced • Multi-Region, Multi-Cloud • AWS, Google Cloud, Azure • Autom a tische Upd a tes und Sc a ling • Query a ble Encryption
  2. Developer Data Platform MongoDB Atlas • Atl a s Se

    a rch (Lucene direkt integriert) • Atl a s Vector Se a rch • Atl a s Stre a m Processing • D a t a Feder a tion • Ch a rts
  3. Enterprise-only Features Community vs. Enterprise • 24/7/365 support • Client-Side

    Encryption (CSFLE/QE) • Kerberos, LDAP • Field Level Red a ction • Auditing
  4. Komponenten • mongod: D a tenb a nk-Server • mongos:

    Router für Sh a rded Cluster • mongosh: MongoDB Shell • Comp a ss: GUI für D a tenb a nken • Atl a s Loc a l CLI: lok a le Entwicklungsumgebungen
  5. Sharded Cluster Deployment Sh a rd 1 (Replic a Set)

    Sh a rd 2 (Replic a Set) Sh a rd 3 (Replic a Set) Con f ig (Replic a Set) Router App Server Router App Server
  6. Sharded Cluster Deployment Sh a rd 1 (Replic a Set)

    Con f ig (Replic a Set) Router App Server Router App Server
  7. Binary JSON BSON { "_id": 1, "name": "Andreas Braun" }

    26 00 00 00 10 5f 69 64 00 01 00 00 00 02 6e 61 |&...._id......na| 6d 65 00 0e 00 00 00 41 6e 64 72 65 61 73 20 42 |me.....Andreas B| 72 61 75 6e 00 00 |raun..|
  8. Typen BSON • string, double, int32, int64, bool, null •

    document, a rr a y • ObjectId, UTCD a teTime, Decim a l128, Regex • Bin a ry (UUID, Encrypted, Compressed, Vector, …) • J a v a script Code • Intern: Timest a mp, Min/M a x Key • Deprec a ted: DBPointer, Unde f ined, Symbol, CodeWScope
  9. Extended JSON BSON 16 00 00 00 07 5f 69

    64 00 68 c1 47 50 8b bc 89 |....._id.h.GP...| f7 c7 04 a7 00 00 |......|
  10. Mehr als SQL Client Libraries • “Treiber” => API für

    D a tenb a nk • PHP: Doctrine MongoDB ODM, L a r a vel • Mongoid (R a ils), Mongoose (Node), Dj a ngo B a ckend
  11. Spezi fi kation Client Libraries • Ap a che- oder

    MIT-Lizenz • O ff ene Spezi f ik a tion • API und Verh a lten ist immer gleich • Kleine V a ri a tionen, z.B. sn a ke_c a se vs. c a melC a se • https://github.com/mongodb/speci f ic a tions
  12. Spezi fi kationen Verbindungsau fb au • Initi a l

    Seedlist Discovery (mongodb+srv) • OP_MSG • Server Discovery a nd Monitoring (SDAM) • H a ndsh a ke • Authentic a tion • Connection Monitoring a nd Pooling (CMAP) • SRV Polling
  13. API Client Libraries • MongoClient • cre a teD a

    t a b a se, listD a t a b a ses, dropD a t a b a se, etc. • MongoD a t a b a se • cre a teCollection, listCollections, dropCollection, etc. • MongoCollection • CRUD, index m a n a gement, etc.
  14. CRUD API Client Libraries • Cre a te • insertOne,

    insertM a ny • Re a d • f ind, f indOne, a ggreg a te • Upd a te • upd a teOne, upd a teM a ny, repl a ceOne • Delete • deleteOne, deleteM a ny
  15. Vorbedingungen PHP • MongoDB Extension • Low-level API und BSON

    libr a ry • pecl: ext-mongodb • pie: mongodb/mongodb-extension • MongoDB Libr a ry • Composer: mongodb/mongodb • Common Drivers API
  16. Ergebnis Dokumente myDb> db.myColl.getIndexes() [ { v: 2, key: {

    _id: 1 }, name: '_id_' } ] myDb> db.myColl.find().pretty() [ { _id: ObjectId('68c16811ce12039d7102f252') } ]
  17. Primary Keys Dokumente • _id ist immer Prim a ry

    Key • Wenn nicht vorh a nden wird neue ObjectId generiert • ObjectId: 4 bytes timest a mp, 5 bytes process unique, 3 byte counter • _id sollte nie ein embedded document sein
  18. Operatoren Queries $collection->findOne(['score' => ['$gt' => 50]]); $collection->findOne(['score' => ['$lte'

    => 100]]); $collection->findOne(['score' => ['$gt' => 50, '$lte' => 100]]);
  19. Embedded Dokumente $collection->insertOne([ 'address' => [ 'street' => 'Hauptstr. 5',

    'city' => 'Musterstadt', 'postCode' => '12345', 'country' => 'Germany', ], ]); $collection->findOne(['address.city' => 'Musterstadt']);
  20. Lists of Embedded Dokumente $collection->insertOne([ 'email' => [ ['type' =>

    'work', 'address' => '[email protected]'], ['type' => 'private', 'address' => '<redacted>'], ], ]); $collection->findOne(['email.type' => 'work']);
  21. Operatoren Updates $collection->updateOne( ['name' => 'Andreas Braun'], [ '$push' =>

    ['email' => '[email protected]'], '$inc' => ['score' => 5], '$pull' => ['phone' => '...'], ], );
  22. fi ndAndModify Updates $collection->findOneAndUpdate( ['name' => 'Andreas Braun'], [ '$push'

    => ['email' => '[email protected]'], '$inc' => ['score' => 5], '$pull' => ['phone' => '...'], ], ['returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER], );
  23. Blog Schema Design writes writes has User ObjectId id string

    name string email string avatar Post ObjectId id ObjectId author string title string text Comment ObjectId id ObjectId author ObjectId post string text
  24. Post Schema Design { "_id": 1, "author": 1, "title": "My

    first post", "text": "This is my first post using MongoDB!" }
  25. API Schema Design { "author": { "name": "alcaeus", "email": "<redacted>"

    }, "title": "My first post", "text": "This is my first post using MongoDB!", "latestComments": [ { "author": { "name": "alcaeus", "email": "<redacted>" }, "text": "Great post!" } ] }
  26. API Post Schema Design { "author": { "name": "alcaeus", "email":

    "<redacted>" }, "title": "My first post", "text": "This is my first post using MongoDB!", "latestComments": [ { "author": { "name": "alcaeus", "email": "<redacted>" }, "text": "Great post!" } ] }
  27. User Schema Design { "_id": 1, "name": "alcaeus", "email": "<redacted>",

    "numPosts": 1, "latestPost": { "_id": 1, "title": "My first post", "numComments": 1 } }
  28. Replica Set Skalierung Prim a ry Second a ry Second

    a ry Second a ry App Writes Re a ds
  29. Replica Set Skalierung Prim a ry Second a ry Second

    a ry Second a ry App Writes Re a ds
  30. Server Selection Skalierung • Re a d Preference • Kon

    f igur a tion • serverSelectionTimeoutMS • serverSelectionTryOnce • he a rtbe a tFrequencyMS • loc a lThresholdMS
  31. Read Preference Mode Skalierung • prim a ry • second

    a ry • prim a ryPreferred • second a ryPreferred • ne a rest
  32. Replica Set Writes Skalierung Prim a ry Second a ry

    Second a ry Second a ry App Write
  33. Replica Set Writes Skalierung Prim a ry Second a ry

    Second a ry Second a ry App Write
  34. Write Concern Mode Skalierung • 0 => YOLO • 1,

    2, …, n • m a jority • <n a me>
  35. Write Concern Gotchas Skalierung • w: <n> ist gef ä

    hrlich • wtimeout ist wichtig • 99% m a jority • 1% YOLO writes
  36. Welche Daten werden gelesen? Read Concern • Def a ult:

    loc a l • Verhindert Lesen von D a ten die zurückgerollt werden könnten
  37. Sharded Cluster Skalierung Sh a rd 1 (Replic a Set)

    Sh a rd 2 (Replic a Set) Sh a rd 3 (Replic a Set) Con f ig (Replic a Set) Router App Server Router App Server
  38. Sharded Cluster Skalierung Sh a rd 1 (Replic a Set)

    Sh a rd 2 (Replic a Set) Sh a rd 3 (Replic a Set) Con f ig (Replic a Set) Router App Server Router App Server
  39. Sharded Cluster Skalierung Sh a rd 1 (Replic a Set)

    Sh a rd 2 (Replic a Set) Sh a rd 3 (Replic a Set) Con f ig (Replic a Set) Router App Server Router App Server
  40. ODM