Posts

MongoDB - Indexing

What is index in MongoDB ? MongoDB provides several of the best data structures for indexing,  such as the classic B-tree , and other additions such as two-dimensional and  spherical GeoSpatial indexes. Whenever a new collection is created, Mongo automatically c reates an index by the _id . These indexes can be found in the system.indexes collection. The following  query shows all indexes in the database: >db.system.indexes.find() Most queries will include more fields than just the _id, so we need to make indexes on those fields. To increase the performance, you need to create index on display filed/s. But before that we need to check if it will improve speed or not. To check that, first check the query without an index. The explain() method is used to output details of a given operations. >db.phones.find({display: "+1 800-5650001"}).explain() { "cursor" : "BasicCursor", "nscanned" : 109999, "nscannedObjects" ...

MongoDB - Strength & Weakness

Mongo’s Strengths Mongo’s primary strength lies in its ability to handle huge amounts of data (and huge amounts of requests) by replication and horizontal scaling . But it also has an added benefit of a very flexible data model, since you needn’t ever conform to a schema and can simply nest any values you would generally join using SQL in an RDBMS anyway. Finally, MongoDB was built to be easy to use. You may have noticed the similarity between Mongo commands and SQL database concepts (minus the server-side joins). This is not by accident and is one reason Mongo is gaining so much mind share from former object-relational model (ORM) users. It’s different enough to scratch a lot of developer itches but not so different it becomes a wholly different and scary monster. Mongo’s Weaknesses How Mongo encourages denormalization of schemas (by not having any) might be a bit too much for some to swallow. Some developers find the cold, hard constraints of a relational datab...

MongoDB - Sharding

Image
One of the central reasons for Mongo to exist is to safely and quickly handle very large datasets. The clearest method of achieving this is through horizontal sharding by value ranges—or just sharding for brevity. Rather than a single server hosting all values in a collection, some range of values are split (or in other words, sharded) onto other servers. For example, in our phone numbers collection, we may put all phone numbers less than 1-500-000-0000 onto Mongo server A and put numbers greater than or equal to 1-500-000-0001 onto a server B. Mongo makes this easier by autosharding, managing this division for you. Sharded cluster picture :

MongoDB help()

> db.help() DB methods: db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [just calls db.runCommand(...)] db.aggregate([pipeline], {options}) - performs a collectionless aggregation on this database; returns a cursor db.auth(username, password) db.cloneDatabase(fromhost) - deprecated db.commandHelp(name) returns the help for the command db.copyDatabase(fromdb, todb, fromhost) - deprecated db.createCollection(name, {size: ..., capped: ..., max: ...}) db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions}) db.createUser(userDocument) db.currentOp() displays currently executing operations in the db db.dropDatabase() db.eval() - deprecated db.fsyncLock() flush data to disk and lock server for backups db.fsyncUnlock() unlocks server following a db.fsyncLock() db.getCollection(cname) same as db['cname'] or db.cname db.getCollectionInfos([filter]) - returns a list that contains t...

MongoDB Introduction

MongoDB is an open-source, cross-platform , document oriented database. It fall under NoSQL database family, written in C++.  It is used for high volume data storage. MongoDB is a highly scalable database. In the real world, companies have created 100+ nodes with around millions of documents within the database. MongoDB has database/s. Mostly multiple DBs in the real world. Each DB has multiple collections. A collection is equivalent to Table of RDBMS. Each collections stores documents. The document is equivalent to records/tuple of RDBMS. Structure of the document is  JSON (JavaScript Object Notation). NO-SQL db is schema-less database.  DataModle within MongoDB allows you to represent hierarchical relationship, store arrays and other complex structure. Document size limit : You could not load any document of size bigger than 16 MB in collection. If you have document bigger than that, you need to use GridFS(an api provided by Mongo...