MongoDB – a NoSQL database

Many NoSQL databases exist in the market today, but MongoDB, by the company with the same name (http://www.mongodb.org/), is the most popular among them. An important distinction between relational and NoSQL databases is that NoSQL databases are schema-less; this means you don't have to define the tables before inserting data. This in itself, of course, adds a lot to the flexibility and agility while using these databases; for example, adding a new field no longer means that you have to alter the table and run the SQL update commands. As there are no SQL queries to be used here, all the data retrieval happens via standard CRUD calls (create, read, update, and delete). In MongoDB, this is known as insert, find, update, and remove. MongoDB presents itself as an open source, distributed, document-oriented database—each data record is actually a document. A table is called a collection in MongoDB. Documents are stored in a JSON-like format called Binary JSON (BSON). BSON documents are objects that contain an ordered list of saved elements; each element comprises of a field name and a value, which is of a specific type. BSON is designed to be more efficient than JSON, both in storage space and reading speed, adding to the performance MongoDB is known for. In a MongoDB database, you can query data not only through keys and secondary keys, but also with ranges and regular expressions; indexes can be applied to pretty much everything, such as 2D and 3D spatial data; there's absolutely no limitation.

To guarantee high availability, a master-slave replication (also called replica-sets) is built. For big data applications, support to spread data across multiple servers (also called auto-sharding) is a key feature, making MongoDB a very scalable solution. Install the latest production release for your system from http://www.mongodb.org/downloads.

This is easy; for details, refer to http://docs.mongodb.org/manual/installation/. A number of binaries are installed in the bin map; among them are mongod, which is the server (or daemon) process, and mongo, which is a command-line client. The http://docs.mongodb.org/manual/tutorial/getting-started/ link is a good tutorial to get started. Start the mongod server process (for example, from c:mongodbin on Windows) before you go further. If all is well, you should see an output, similar to the following, on the console:

Fri Aug 23 10:57:19.256 [initandlisten] MongoDB starting : pid=1568 port=27017 db
path=datadb 64-bit host=predator
Fri Aug 23 10:57:19.258 [initandlisten] db version v2.4.5

To close the MongoDB server safely, issue Ctrl + C in the console. Before using a Dart driver, let's get acquainted a bit with the Mongo shell, which works with JavaScript and communicates directly with MongoDB. Open a console window and start the mongo command. The following output appears, telling us that we are using the default database test and showing us a prompt:

  MongoDB shell version: 2.4.5
  connecting to: test
  >  

Suppose we want to save stock data; for example, GOOG (the symbol), Google Inc. (the company's name), and its current rating of 13. We will call our database invest. Switch to that database with use invest, which returns switched to db invest. At this point, the database doesn't really exist, as it doesn't contain any data. In MongoDB, data is stored in collections, allowing you to separate documents if required. Let's create a document and store it as a new collection named stocks:

db.stocks.save({symbol:"GOOG",name:"Google Inc.",rating:13});

Save some other data as follows:

Symbol

Company

Current rating

MSFT

Microsoft Technologies

7

KOG

Kodiak Oil & Gas Corp

11

AAPL

Apple Inc

11

CSCO

Cisco Systems

12

Our collection now contains five documents. Retrieve them with db.stocks.find():

{"_id" : ObjectId("521df951abd1215f4673c8ed"), "symbol" : "GOOG", "name" : "Google Inc.", "rating" : 13 }
{ "_id" : ObjectId("521df997abd1215f4673c8ee"), "symbol" : "MSFT", "name" : "Microsoft Technologies", "rating" : 7 }
{ "_id" : ObjectId("521df9caabd1215f4673c8ef"), "symbol" : "KOG", "name" : "Kodiak Oil & Gas Corp", "rating" : 10 }
// 2 other documents left out     

The _id attribute is a unique identifier generated by MongoDB and it will be different in your result. Documents can be way more complex than these, storing various datatypes including strings, integers, floats, dates, arrays, and other objects. Suppose we only want to see the stocks with a rating greater than 11; the query gives us GOOG and CSCO:

db.stocks.find({rating:{$gt:11}});

For example, sorting on the symbol is also easy:

db.stocks.find().sort({symbol: 0});

Add .count() at the end of any find command that will give the number of documents found. If Microsoft's new CEO comes along and the company's rating rises to 12, how do we change it in our database via the shell? First, we must get a variable reference to the document we want to change:

var ms= db.stocks.findOne({symbol: "MSFT"});

Then, we make the change as we would in the code:

ms.rating = 12;

Then, we save the change with:

db.stocks.save(ms);

Verify with find() that the change is stored. You can create an index in the symbol field with:

db.stocks.ensureIndex({symbol: 1});

To create a backup, issue the mongodump command. To explore the shell and its commands further, visit the MongoDB website. The following screenshot illustrates the Mongo shell displaying the stocks collection:

MongoDB – a NoSQL database

The Mongo shell showing the stocks collection

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.34.39