pokemon are cool
run in docker container and expose mongo port 27017 on localhost
docker run -d --rm --hostname mongo --name mongo -p 27017:27017 mongo
ways to connect:
- GUI
- programmatic via a Driver (e.g. Nodejs driver https://mongodb.github.io/node-mongodb-native/)
- cli access (MongoDb shell)
docker exec -it <CONTAINER_ID> /bin/bash
thenmongo
- or directly
docker exec -it <CONTAINER_ID> mongo
- physical database, i.e. the mongodb server
- logical databases (db) - e.g. for each application/service a different database
- collections - e.g all users
- namespace = a logical db + a collection
- documents - e.g. a single user
Relational Dbs (SQL) | Non-relational (document-based, No-SQL) |
---|---|
schema-based (think classes) | schema-less (think json object) |
tables | collections |
records | documents |
data format is pretty much known up front | rapid prototyping |
very fast | fast but in some use-cases... |
normalized | not normalized |
avoids inconsistencies | accepts/manually resolves inconsistencies |
(almost) no duplication | duplication somtimes accepted |
good for communication /forced to document (by schema) | (by default) no documentation, just data |
"type-safe" | flexible |
migrations needed | no migrations since no schema, but for data |
show dbs
use pokemonDB
db.createCollection('pokemon')
db.createCollection('trainers')
show collections
db.trainers.find({name:"Ashe"})
db.trainers.findOne({name:"Ashe"})
db.trainers.update({name:"Ashe"},{$set:{level:7}})
db.trainers.update({name:"Ashe"},{name:"Ash Clown"})
const t = db.trainers
t.update({name:"Misty"},{},{upsert:true})
t.deleteMany({name:{$exists:false}})
t.deleteOne({_id:ObjectId("6073170842691fc14f93caa4")})
- CRUD
- no-sql vs sql based Dbs
- https://en.wikipedia.org/wiki/Database_normalization
- schema for mongo -> mongoose
- Mongo Queries
- MongoDb NodeJS driver
- MongoDb NodeJS API docs
- TypeScript Starter Template
- Indices - make things fast
- Aggregation Pipelines - array.reduce() for lists of documents - can also be done completely on application level (TS/JS), but DB is probably faster