Use different mongodb instances in mongoid

By default, we save all the collections in one mongodb instance, or replicate/shard all of them into different mongodb instances. But what if saving a special collection into one mongodb instance, and the other collections into the other mongodb instance?

This is what I need to do with a mongoid project several weeks before. In common we just define a mongodb instance in the config/mongoid.yml, yep, you can define only one instance for one environment just like activerecord, the define the different mongodb instance in model.

class Ad
  include Mongoid::Document

  collection.master = Mongoid::Collections::Master.new(
    Mongo::Connection.new(AD_DB_HOST, AD_DB_PORT).db(AD_DB_NAME),
    AD_COLLECTION_NAME
  )
end

change the AD_DB_HOST, AD_DB_PORT, AD_DB_NAME and AD_COLLECTION_NAME with your mongodb configuration, the following is my configuration,

AD_DB_HOST => 'localhost'
AD_DB_PORT => 27018
AD_DB_NAME => 'ad_developmet'
AD_COLLECTION_NAME => 'ads'

and it would be better move these configurations into a config file.

Now when saving or reading the ads collection, it uses localhost:27018/ad_development, and the other collections use localhost:27017/project_development.

Posted in  mongodb mongoid


Drop tmp collections in Mongodb

I'm trying mongodb map/reduce functionality with mongoid these days. I find there is a tmp.mr.mapreduce_ddd_ddd collection created after each map/reduce operation, it's ok that these tmp collections are used to hold output of map/reduce operation.

From mongodb document, it's said the temp collections will be cleaned up when the client connection closes or when explicitly dropped. But I never see these temp collections are dropped, when I print show collections, there are too many temp collections annoyed me, I decided to drop these temp collections explicitly.

db.system.namespaces.find({name: /tmp.mr/}).forEach(function(z) {
  try{
    db.getMongo().getCollection( z.name ).drop();
  } catch(err) {}
});

It finds all the namespaces whose names contain tmp.mr, if so, drop the collections.

Posted in  mongodb


JQuery Plugin Template

JQuery is one of the most important javascript framework I used, besides default jquery apis, I use a lot of jquery plugins to improve the web interactive, such as auto complete, dropdown menu, chart and so on.

I will be likely to write some jquery plugins or hack others' jquery plugins, The following is the template for a jquery plugin that I learned from "jQuery 1.4 Plugin Development"

(function($) {
  $.fn.pluginName = function(options) {
    var defaults = {
      ......
    }

    var o = $.extend({}, defaults, options);

    return this.each(function() {
      var e = $(this);
      ......
    });
  }
})(jQuery);

the structure (function($) {...})(jQuery) can protected the conflict of $ sign which is used by other javascript framework.

$.extend({}, defaults, options) provides a flexible way to tweak your plugin, options can override defaults, but not change the value of defaults.

return this.each(function() {...}) promises your plugin functions are chainable, like the default jquery apis.

Posted in  jquery


盛大创新院分享

昨天去盛大创新院做分享,主要是介绍我目前正在做的网站,rails-bestpractices.com

盛大创新院还是很不错的地方,只可惜昨天他们一些做rails的牛人都在开会,失去了一个被拍砖的机会

Posted in  presentation


将table的数据导出为csv

项目中经常会有这样的usecase,把一个table中的数据导出为csv,用fastercsv这个gem可以快速完成这个功能。

首先放一个导出csv的链接

= link_to 'Export to CSV', participants_path(:format => :csv)

然后在controller中生成相应的csv,并发送给用户

def index
  @participants = Participant.all

  respond_to do |format|
    format.csv {
      participants_csv = FasterCSV.generate do |csv|
        csv  ["First Name", "Last Name", "Age", "Gender", "Address", "Phone", "Email"]
        @participants.each do |p|
          csv  [p.first_name, p.last_name, p.age, p.gender, p.address, p.phone, p.email]
        end
      end
      send_data participants_csv, :type => 'text/csv', :filename => 'participants.csv'
    }
  end
end

其中用FasterCSV快速创建好csv数据,再通过send_data发送给客户端就可以了

Posted in  rails


Fork me on GitHub