class Wukong::Load::MongoDBLoader
Loads data into MongoDB.
Uses the 'mongo' gem to connect and write data.
Allows loading records into a given database and collection. Records can have fields `_database` and `_collection` which override the given database and collection on a per-record basis.
Records can have an `_id` field which indicates an update, not an insert.
The names of these fields within each record (`_database`, `_collection`, and `_id`) can be customized.
Attributes
client[RW]
The Mongo::MongoClient we'll use for talking to MongoDB.
Public Instance Methods
load(record)
click to toggle source
Load
a single record into MongoDB.
If the record has an ID, we'll issue an update, otherwise an insert.
@param [record] Hash
# File lib/wukong-load/loaders/mongodb.rb, line 79 def load record id = id_for(record) if id res = collection_for(record).update({:_id => id}, record, :upsert => true) if res['updatedExisting'] log.info("Updated #{id}") else log.info("Inserted #{id}") end else res = collection_for(record).insert(record) log.info("Inserted #{res}") end end
setup()
click to toggle source
Creates the client connection.
# File lib/wukong-load/loaders/mongodb.rb, line 58 def setup begin require 'mongo' rescue LoadError => e raise Error.new("Please ensure that the 'mongo' gem is installed and available (in your Gemfile)") end h = host.gsub(%r{^http://},'') log.debug("Connecting to MongoDB server at #{h}:#{port}...") begin self.client = Mongo::MongoClient.new(h, port) rescue => e raise Error.new(e.message) end end