root@omarbelkady:~$ sudo apt install mongodb
root@omarbelkady:~$ npm install mongoose --save
- MongoDB stores docs(aka rows) in what are called collections(table)
- MongoDB stores data records as Binary Documents. BSON is the binary(fav lev of program for @alanngo) repr of JSON docs.
Type | Alias |
---|---|
Double | double |
String | string |
Object | object |
Array | array |
ObjectId | objectId |
Boolean | bool |
Date | date |
Null | null |
Regular Expression | regex |
Javascript | javascript |
32 Bit Integer | Int |
64 Bit Integer | long |
Decimal128 | decimal |
Min and Max Key | minKey maxKey |
- Client makes a request to the server
- Server connects to the DB
- Data is retrieved from the DB and it displays the data(if available) in the browser(Client)
root@omarbelkady:~$ db
root@omarbelkady:~$ db.getName();
mongod
mongo
show dbs;
use cstsffb
db.createCollection("76lanc6", {YOUR_OPTIONS_GO_HERE});
show collections
insert(data)
db.DBNAME.insert(JSONObJECTYouWANTTOADD);
db.DBNAME.insertMany(JSONObJECTYouWANTTOADD);
insertOne(data, differentOptionsYouWant);
insertMany(data, differentOptionsYouWant);
insertMany([{},{},{}])
- find takes two arguments the query criteria and the projection
- limit to 7 students
- age and name can be referred to as the columns in SQL
db.collectionNAME.find(
{ age: { $gt: 21} },
{ name: 1, address: 1},
).limit(7).pretty();
db.collectionNAME.find({fname: "Omar"}).pretty();
db.collectionNAME.find({fname: "Omar"}, {fname: 1, lname: 1, gender: 1}).pretty();
db.collectionNAME.find({fname: "Omar"}, {fname: 1, lname: 1, likesJava: 0}).pretty();
find(filter, options)
findOne(filter, options)
updateOne(filter,data,options)
updateMany(filter,data,options)
db.collectionNAME.update([_id: ObjectId("qZvCf/4+AcPdqHoNMkrrXbsz66H3NOMkzbDzF+Uv9HI=")]);
replaceOne(filter, data, options)
deleteOne(filter, options)
deleteMany(filter, options)
root@omarbelkady:~$ db.dropDatabase();
root@omarbelkady:~$ db.DBNAME.drop();
root@omarbelkady:~$ db.animals.drop();
root@omarbelkady:~$ db.help();
["key":"value"]
$eq
$gt
$gte
$["key":{$in: [arrOfValues] } ]
$lt
$lte
$ne
$nin
$and: { [], [] }