Class: MongoDatabase

Inherits:
Object
  • Object
show all
Defined in:
lib/winston_mongodb_rails/mongo_database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MongoDatabase

Returns a new instance of MongoDatabase.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/winston_mongodb_rails/mongo_database.rb', line 5

def initialize(options = {})
    
  options = options.symbolize_keys  
    
    
  if ['production', 'staging'].include? Rails.env

    @connection = Mongo::ReplSetConnection.new(options[:replicaset], :read => :secondary, :name => options[:replicaset_name])
    @connection.add_auth(options[:database], options[:username], options[:password])
    @connection.add_auth('admin', options[:username], options[:password])
  else
    url = "mongodb://#{options[:host]}"
    @connection = Mongo::Connection.from_uri(url)
  end

end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



3
4
5
# File 'lib/winston_mongodb_rails/mongo_database.rb', line 3

def connection
  @connection
end

Instance Method Details

#allObject



22
23
24
# File 'lib/winston_mongodb_rails/mongo_database.rb', line 22

def all
  @connection.database_names
end

#capped_records(database, collection, conditions, last = Time.now) ⇒ Object

Need to do some sort of skip and last chat.



69
70
71
72
73
74
# File 'lib/winston_mongodb_rails/mongo_database.rb', line 69

def capped_records(database, collection, conditions, last = Time.now)
  db = @connection.db(database)
  coll = db.collection(collection)
  conditions = conditions.merge({"timestamp" => {"$lt" => last}})
  coll.find(conditions).sort([["$natural", "-1"]]).limit(50)
end

#collections(database_name) ⇒ Object



26
27
28
29
# File 'lib/winston_mongodb_rails/mongo_database.rb', line 26

def collections(database_name)
  db = @connection.db(database_name)
  db.collection_names
end

#distinct(database, collection, field) ⇒ Object



31
32
33
34
35
# File 'lib/winston_mongodb_rails/mongo_database.rb', line 31

def distinct(database, collection, field)
  db = @connection.db(database)
  coll = db.collection(collection)
  coll.distinct(field.to_s)
end

#find(database, collection, id) ⇒ Object



86
87
88
89
90
# File 'lib/winston_mongodb_rails/mongo_database.rb', line 86

def find(database, collection, id)
  db = @connection.db(database)
  coll = db.collection(collection)
  coll.find_one(:_id => BSON::ObjectId(id))
end

#range_records(database, collection, conditions, first = Time.now, last = Time.now, limit = 2000) ⇒ Object

Need to do some sort of first and last chat.



78
79
80
81
82
83
84
# File 'lib/winston_mongodb_rails/mongo_database.rb', line 78

def range_records(database, collection, conditions, first = Time.now, last = Time.now, limit = 2000)
  db = @connection.db(database)
  coll = db.collection(collection)
  conditions = conditions.merge({"timestamp" => {"$lte" => last, "$gte" => first}})

  baseQuery = coll.find(conditions).sort([["$natural", "-1"]]).limit(limit)
end

#records(database, collection, last = Time.now) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/winston_mongodb_rails/mongo_database.rb', line 38

def records(database, collection, last = Time.now)
      
  db = @connection.db(database)
  coll = db.collection(collection)

  if row = coll.find().sort("_id", -1).limit(1).first
    keys = row.keys
    if keys.include?('updated_at')
      find_options = {"updated_at" => {"$lt" => last}}
      sort_options = [['updated_at', 'descending']]

    elsif keys.include?('created_at')
      find_options = {"created_at" => {"$lt" => last}}
      sort_options = [['created_at', 'descending']]
    else
      find_options = {}
    end
  else
    find_options = {}
  end
      
  query = coll.find(find_options).limit(25)

  if sort_options
    query.sort(sort_options)
  else
    query
  end
end

#update(database, collection, id, params) ⇒ Object



92
93
94
95
96
97
# File 'lib/winston_mongodb_rails/mongo_database.rb', line 92

def update(database, collection, id, params)
  db = @connection.db(database)
  coll = db.collection(collection)
  s_params = params.stringify_keys
  coll.update({"_id" => BSON::ObjectId(id)}, {"$set" => s_params}, :safe => true)
end