Home

  1. MongoDB Melbourne 2012

    Recently, I attended MongoDB Melbourne 2012 at The Marriott in the CBD. The day featured 10gen talking about MongoDB plus a few interesting lightning talks.

    I really enjoyed the day and took a few notes that I found interesting:

    Schema Design with Emily Stolfo

    • Emily is the current maintainer of the Ruby driver
    • Embedded documents great for immutable data. A book’s ISBN is a good example
    • Emily’s presentation style was great, presenting a suboptimal approach to querying data then presenting the optimal or ‘MongoDB’ way
    • A MongoDB document can be no greater than 16MB

    Indexing and Query Optimization with Stephen Steneker

    I really enjoyed this presentation as there was so many juicy tips and tricks!

    • EXPLAIN support for querying via .explain()
    • When creating indices, 1 = ascending, -1 = descending
    • Use ensureIndex({ background: true }) to index without blocking (there are some caveats however)
    • Use ensureIndex({ expireAfterSeconds: <int>}) to remove documents after a certain number of seconds with a cleanup precision of 60 seconds (TTL)
    • There is support for Capped collections which allows one to define a collection of a particular size and once full, oldest documents are removed
    • Supports 2D Geospatial indexing ($near, $within, $polygon)
    • db.setProfilingLevel(n, slowms=100ms) n = 0, 1, 2 – default, all queries > 100ms logged
    • When you see BasicCursor in the EXPLAIN query plan, that’s a full table scan
    • Calling explain(true) gives more detail
    • Query optimiser samples 1000 queries and decides best approach
    • When using ^ with Regex searching, this will take advantage of any indices
    • ElasticSearch River plugin

    Operating MongoDB in the Cloud with Adam Comerford

    Shell Games with Stephen Steneker

    Published:
    Categories: MongoDB articles

  2. Git merge squash tip

    I tend to create git branches when working on a new piece of functionality and merge into master at various points along the way. Whilst this approach does have some potential pitfalls (deviating too far from master, conflict hell) I believe when managed and used correctly, feature branches yield many benefits.

    Just recently, I wanted to merge the changes I had made in my feature branch am-feature1 into master but as a single commit with a more succinct commit message and came across the --squash argument to git merge:

      $ git status
      # On branch master
      nothing to commit (working directory clean)
    
      $ git merge --squash am-feature1
      Updating 4706c64..761b469
      Fast-forward
      Squash commit -- not updating HEAD
       file2.txt |    1 +
       file3.txt |    1 +
       2 files changed, 2 insertions(+)
       create mode 100644 file2.txt
       create mode 100644 file3.txt
    
      $ git status
      # On branch master
      # Changes to be committed:
      #   (use "git reset HEAD <file>..." to unstage)
      #
      # new file:   file2.txt
      # new file:   file3.txt

    I was then free to commit the changes and type a single message to cover the detail of the change.

    Published:
    Categories: Git articles

  3. Logrotate frequency and size

    I wanted to rotate a log file daily OR if the size was greater than 2MB:

    /etc/logrotate.d/massive

    /var/log/massive.log {
        daily
        size 2M
        rotate 7
    }

    It turns out this is not possible as the size options overrides the daily,weekly,etc option (and vice-versa, whichever options appears last):

    $ sudo /usr/sbin/logrotate -dv /etc/logrotate.conf
    rotating pattern: /var/log/massive.log  2097152 bytes (7 rotations)
    ..

    After changing /etc/logrotate.d/massive

    /var/log/massive.log {
        size 2M
        daily
        rotate 7
    }
    $ sudo /usr/sbin/logrotate -dv /etc/logrotate.conf
    rotating pattern: /var/log/massive.log  after 1 days (7 rotations)
    ..

    Running /usr/sbin/logrotate with -dv was very helpful in diagnosing what was going on without making any changes.

    Published:
    Categories: Devops articles

  4. Dropping a stubborn database in Mongo

    Due to a typo in my Rails database.yml file, I had a Mongo DB that was badly named (see errbit%):

    > show dbs
    local (empty)
    errbit 0.203125GB
    errbit% 0.203125GB

    Dropping the database using the usual command appeared to work:

    > use 'errbit%'
    switched to db 'errbit%'
    > db.dropDatabase()
    { "dropped" : "'errbit%'", "ok" : 1 }

    But upon closer inspection, it was still there :(

    > show dbs
    local (empty)
    errbit 0.203125GB
    errbit% 0.203125GB

    You can get around this by using db.getSiblingDB("<BAD DB NAME>"):

    > var bad_db = db.getSiblingDB("errbit%")
    > bad_db.dropDatabase()
    { "dropped" : "errbit%", "ok" : 1 }
    > show dbs
    local (empty)
    errbit 0.203125GB

    Published:
    Categories: MongoDB articles