CS6320:  SW Engineering of Web Based Systems

 

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
*** or why use relational instead

  1. you can easily add new 'columns' and 'tables'
  2. speed (****not really true over professional RDMS***)
  3. sharding
  4. document matches more closely to an object than a set of relational tables so mapping becomes easier
  5. It broadens the mind
  1. (the lack of / different vision on) durability (read http://www.mikealrogers.com/2010/07/mongodb-performance-durability)
  2. No transactions
  3. No constraints
  4. Aggregation with MapReduce is slow and you need to write code for something like group-by
  5. Reporting is harder, the developer defines the relations but business analysts can't build their own queries, they can't for example do a 'minus' ('except' in sql server lingo)
  • No more SQL statements
  • Your database resembles your classes not the other way around
  • Your "schema" is more flexible
  • Scales well
  • Very easy to get started with
  • < opinion>It's cool< / opinion>
 
  • Flexible to change data format/schema during development
  • Simpler (in many ways) product and management tools 

 

  • Doing extensive reporting on the stored data can be harder when using MongoDB or any document database and some use cases have been combining RDBMS and document-db for that purpose.
  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

Connecting

var 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
// Insert a single document
var r = yield db.collection('inserts').insertOne({a:1});

assert.equal(1, r.insertedCount);

 

// Insert multiple documents
var r = yield db.collection('inserts').insertMany([{a:2}, {a:3}]);

assert.equal(2, r.insertedCount);

 

//FIND -like read
// Get first two documents that match the query
// Get the collection
var col = db.collection('find');
var docs = yield col.find({a:1}).limit(2).toArray(); assert.equal(2, docs.length);
//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
/// Get the updates collection
var col = db.collection('updates');
// Insert a single document
var r = yield col.insertMany([{a:1}, {a:2}, {a:2}]);
assert.equal(
3, r.insertedCount);

// Update a single document
var r = yield col.updateOne({a:1}, {$set: {b: 1}});
assert.equal(
1, r.matchedCount);
assert.equal(
1, r.modifiedCount);

// Update multiple documents
var r = yield col.updateMany({a:2}, {$set: {b: 1}});
assert.equal(
2, r.matchedCount);
assert.equal(
2, r.modifiedCount);

 

//DELETING documents
// Get the removes collection
var col = db.collection('removes');

// Insert a single document
var r = yield col.insertMany([{a:1}, {a:2}, {a:2}]);
assert.equal(
3, r.insertedCount);

// Remove a single document
var r = yield col.deleteOne({a:1});
assert.equal(
1, r.deletedCount);

// Delete multiple documents
var r = yield col.deleteMany({a:2});
assert.equal(
2, r.deletedCount);

 

//FIND AND UPDATE OR FIND AND DELETE
// Modify and return the modified document

var r = yield col.findOneAndUpdate({a:1}, {$set: {b: 1}}, { returnOriginal: false , sort: [[a,1]] , upsert: true });
assert.equal(
1, r.value.b);

// Remove and return a document
var r = yield col.findOneAndDelete({a:2});
assert.ok(r.value.b ==
null);

// Close connection
db.close();

 

 

 

© Lynne Grewe