Understanding MongoDB: BtreeCursor

Applicable to Mongo 2.4

MongoDB uses cursors to iterate over resultant documents from a query execution. There are cursor for document records and index records. MongoDB uses Btree to manage document indexes and uses class BtreeCursor to implement cursor management of indexes.

  • Code is under ./src/mongo/db/btreecursor.cpp.
  • class BtreeCursorImpl is implementation of class BtreeCursor
  • Cursor is initialized using BtreeCursorImpl( NamespaceDetails* a , int b, const IndexDetails& c )
  • A Btree bucket has ordered pairs of key=BSON(field) and value=DiskLoc of data record
  • Cursor iterates over each pair of <key, value=””>
  • BtreeCursor requires implementation of the following:
    • locate a key and return its DiskLoc (_locate())
    • Return BSON(key) for a given offset keyofs (keyNode())
    • Move cursor in a backward/forward direction from a given key, offset (_advanceTo())
    • Since cached info about an index could get changed during iteration (checkLocation())



Mongo DB: Good to know things

  • Mongo DB is a No-SQL, free, open-source solution that is highly scalable, highly available and high performance solution.
  • Engine is coded in C++
  • Works in a client-server model
  • Major components:
    • mongod: The storage server
    • mongos: The sharding server
    • config server(s):
      • Stores metadata that accomplish sharding
      • Is actually a mongod process
  • Mongo provides write operations durability with journaling (write ahead logging)
  • User data is seen as a database of collection of records
    • Collection is roughly similar to a table in RDBMS
    • Record could be map to a row in a table (incorrect but helps understanding)
  • Mongo stores data in BSON format (on-wire and on-disk)