MongoDB - a quick look
-
NO SQL "database" or sometimes called a "document" database
Leveraging your knowledge of traditional databases
-
collection = like database table
document = entry /row in collection = like database row
field = like a column but, --there is no ordering or notion of required
field _id = primary key
Each document can have different fields of data!!!
A note about the special field _id
every document in a collection will be assigned a unique value of _id.
it is like it's primary key
Some andedotal pros/cons of a Document Database (like MongoDB) over a Relational Database (like Oracle, MySQL)
Pros of Document Database |
Cons of Document Database |
|
|
|
|
|
|
Applications that require complex, multi-row transactions (e.g., a double-entry bookkeeping system) |
Mongoose in MongoDB --adding more structure --to make program reliability better?
"provides elegant MongoDB object modeling for Node.js"
MongoDB and NodeJS
Connectingvar mongodb = require('mongodb');
// Standard URI format: mongodb://[dbuser:dbpassword@]host:port/dbname
// GO TO mLab.com account to see what YOUR database URL is
//CHANGE the url so it is correct for your account
var uri ='mongodb://YOUR_LOGIN:YOURPASSWROD@WHATEVER.mlab.com:xxxxx/dnName';
//using mongodb module
mongodb.MongoClient.connect(uri, function(err, client){
if(err) throw err; var theDatabase = client.db('dbName'); var db = theDatabase.collection('collenctionName'); // NOW DO WHAT YOU WANT -- CRUD db.close(); //close database connection when you are done } |
||||||||
CRUD - data manipulation (Create Read Update Delete) -- read mongoDB site for more including bulk writes
********* 2 great sources:
Insert --like create
FIND -- like read
|
//Example that finds ALL the documents in the dogs collection & gets results in an array you can cycle through db.collection("dogs").find({}).toArray(function(err, result) { for(i=0; i< result.length; i++) |
//Example that finds all the dogs with age 3 var query = {age: 3}; //query is comma separated conditional --JSON format |
//FIND example--where cycle through the returned documents as a cursor type datastructure rather than array // Save find results in local variable cursor var cursor = col.find({age:1}).limit(2); //find the first 2 dog documents ("entries") that have the field of age=1 // Iterate over retrieved documents while(yield cursor.hasNext()) { var doc = yield cursor.next(); console.dir(doc); } |
// Example that finds all the dogs in a collection and then applies sort on them based on alphanumeric ordering on the name field var mysort = { name: 1 }; //specifies alphanumeric sort on field name |
VISIT the API page for more examples including REGULAR expresssions for finding certain string patterns https://mongodb.github.io/node-mongodb-native/markdown-docs/queries.html |
Update
//UPDATING //first setup the collection var col = db.collection('dogs'); var r = yield col.updateOne({name:"fido"}, {$set: {age: 2}}); //change fido dog's age to 2 |
Delete
//first setup the collection var col = db.collection('dogs'); var r = yield col.insertMany([{name:"fido", age:1}, {name:"spot", age:2}, {name:"rex", age:2}]);
|