MongoDB Short Learning Curve 263 2526 56837 727225
root@omarbelkady:~ $ sudo apt install mongodb
Package Used To Interract With 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
How Do We Connect To MongoDB
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)
Display The Name of the current db
root@omarbelkady:~ $ db.getName ();
Show The List of Databases
Switch to the cstsffb database
db.createCollection("76lanc6", {YOUR_OPTIONS_GO_HERE});
Show the list of Database Collections
Insert data into a document
Insert a student into a collection a js object with a js extension
db.DBNAME.insert(JSONObJECTYouWANTTOADD);
Insert Many student into the collection a js object with a js extension
db.DBNAME.insertMany(JSONObJECTYouWANTTOADD);
insertOne(data, differentOptionsYouWant);
insertMany(data, differentOptionsYouWant);
Show the data within a collection
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();
Return Specific Fields of Omar
db.collectionNAME.find({fname: "Omar"}, {fname: 1, lname: 1, gender: 1}).pretty();
Exclude Specific Fields of Omar
db.collectionNAME.find({fname: "Omar"}, {fname: 1, lname: 1, likesJava: 0}).pretty();
Find All The Matching Documents
Return the first Document That has the best Match
Update one document within the collection
updateOne(filter,data,options)
Update Many Documents within the collection
updateMany(filter,data,options)
Update A Documents within the collection
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 ();
Arguments passed in are to filter collection
Return A Match That is Equal to The Specific Value I inputed
Return A Match That is Greater than The Specific Value I inputed
Return A Match That is Greater than Or Equal To The Specific Value I inputed
Return A Match if there is any values supplied in the array
$["key":{$in: [arrOfValues] } ]
Return A Match That Is less than the value I supplied
Return A Match That is Less than Or Equal To The Specific Value I inputed
Return A Match That Is not equal to the value I supplied
Return A Match That Has NONE OF the value I supplied