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, db) {
if(err) throw err; // NOW DO WHAT YOU WANT -- CRUD } |
CRUD - data manipulation (Create Read Update Delete) -- read mongoDB site for more including bulk writes
//INSERT -like create assert.equal(1, r.insertedCount);
// Insert multiple documents assert.equal(2, r.insertedCount);
//FIND -like read
// Get first two documents that match the query
//option 2 Find
// Get the cursor
var cursor = col.find({a:1}).limit(2);
// Iterate over the cursor
while(yield cursor.hasNext()) {
var doc = yield cursor.next();
console.dir(doc);
}
//UPDATING
//DELETING documents
//FIND AND UPDATE OR FIND AND DELETE // Close connection |