MongoDB Indexing Part 3


Sort Direction for index

When we created the index, we specified the sort direction for the index, in this case both ascending. Sorting on this indexes would work only is they are both indexed ascending or descending. But if you ix the direction of either fields, mongo will skip indexing all together.

MongoDB Indexing Part 3

Advertisements

MongoDB Indexing Part 2


Multi Term Query

db.animals.find({name:’cat’, tags:’land’}).explain()

— Looking for the name ‘cat and the tag ‘land’. the result

vlcsnap-2016-10-06-05h11m34s177

As you can see there is an index and the same index is being used.

MongoDB Indexing Part 2

MongoDB Indexing : Part 1


The Basics

Lets say you have a statement like

db.foo.find({x:2})

What does the server do in order to find this document?  It does an innocuous  for-each loop looking for the values, document by document ( assuming the documents were placed contiguously and there was no other logic involved in storing the document) until it finds the value (trying to make a point so I am over simplifying it).

Obviously what I have just stated is a very slow & inefficient way for searching the document. The solution is to create an Index ( as in all other modern databases). Each document has its own location on disk and an index logically holds mapping to those locations from field values.

MongoDB Indexing : Part 1

MongoDB CRUD Operations


Previous Article: Data Storage Internals

Create Documents

Assuming you are in the test database in Mongo and you want to find out the tables (There are no tables in Mongo) and collections (only collections) in the database.

>DB
Test
>Show collections
>db.Foo.Save({_id:1, x:10})
>db.Foo.find()
{"_id":1, "x":10}
>Show collections()
foo
system.indexes
>db.Bar.Save({_id:1, x:10})
>show collections()
Foo
Bar
System.indexes

Since it is a test DB there are no collections.

Remember that collections in Mongo defines the scope of interaction with documents

MongoDB CRUD Operations

MongoDB : Replica Sets


Previous: MongoDB the shell unpeeled

Typically we have a primary server along with a backup server. If the primary fails and if you are well prepared ( and if everything goes well) we can restore the primary from the  backup.

MongoDB uses the concept of replication which is a much better alternative to traditional backup solutions. Mongo uses an arrangement called the Relica Set. The members of the Relica Set are a Primary DB, Secondary DB(s) and an Arbiter DB (potentially). Each of these servers play a specific role in the Replica Set

MongoDB : Replica Sets