require "logstash/outputs/base"
require "logstash/namespace"
require "time"

class LogStash::Outputs::Mongodb < LogStash::Outputs::Base

  config_name "mongodb"
  plugin_status "beta"

  # your mongodb host
  config :host, :validate => :string, :required => true

  # the mongodb port
  config :port, :validate => :number, :default => 27017

  # The database to use
  config :database, :validate => :string, :required => true

  config :user, :validate => :string, :required => false
  config :password, :validate => :password, :required => false

  # The collection to use. This value can use %{foo} values to dynamically
  # select a collection based on data in the event.
  config :collection, :validate => :string, :required => true

  public
  def register
    require "mongo"
    # TODO(petef): check for errors
    db = Mongo::Connection.new(@host, @port).db(@database)
    auth = true
    if @user then
      auth = db.authenticate(@user, @password.value) if @user
    end
    if not auth then
      raise RuntimeError, "MongoDB authentication failure"
    end
    @mongodb = db
  end # def register

  public
  def receive(event)
    return unless output?(event)

    @tmphash=event.to_hash
    @tmphash['@timestamp']=Time.now
    @mongodb.collection(event.sprintf(@collection)).insert(@tmphash)
  end # def receive
end # class LogStash::Outputs::Mongodb

require "logstash/outputs/base"
require "logstash/namespace"

class LogStash::Outputs::Mongodb < LogStash::Outputs::Base

  config_name "mongodb"
  plugin_status "beta"

  # your mongodb host
  config :host, :validate => :string, :required => true

  # the mongodb port
  config :port, :validate => :number, :default => 27017

  # The database to use
  config :database, :validate => :string, :required => true

  config :user, :validate => :string, :required => false
  config :password, :validate => :password, :required => false

  # The collection to use. This value can use %{foo} values to dynamically
  # select a collection based on data in the event.
  config :collection, :validate => :string, :required => true

  public
  def register
    require "mongo"
    # TODO(petef): check for errors
    db = Mongo::Connection.new(@host, @port).db(@database)
    auth = true
    if @user then
      auth = db.authenticate(@user, @password.value) if @user
    end
    if not auth then
      raise RuntimeError, "MongoDB authentication failure"
    end
    @mongodb = db
  end # def register

  public
  def receive(event)
    return unless output?(event)

    @mongodb.collection(event.sprintf(@collection)).insert(event.to_hash)
  end # def receive
end # class LogStash::Outputs::Mongodb

Revert "MongoDB 2.2 has a number of interesting features, one of which is the ability to expire data from a collection based on timestamp (TTL). This feature however only works if the field is of datatime type."
Revert "MongoDB 2.2 has a number of interesting features, one of which is the ability to expire data from a collection based on timestamp (TTL).  This feature however only works if the field is of datatime type."

This reverts commit 6e3d200e1d0f299eef6745439029f30f146745f5.
