Class: MongoDatabase
- Inherits:
-
Object
- Object
- MongoDatabase
- Defined in:
- lib/winston_mongodb_rails/mongo_database.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
- #all ⇒ Object
-
#capped_records(database, collection, conditions, last = Time.now) ⇒ Object
Need to do some sort of skip and last chat.
- #collections(database_name) ⇒ Object
- #distinct(database, collection, field) ⇒ Object
- #find(database, collection, id) ⇒ Object
-
#initialize(options = {}) ⇒ MongoDatabase
constructor
A new instance of MongoDatabase.
-
#range_records(database, collection, conditions, first = Time.now, last = Time.now, limit = 2000) ⇒ Object
Need to do some sort of first and last chat.
- #records(database, collection, last = Time.now) ⇒ Object
- #update(database, collection, id, params) ⇒ Object
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( = {}) = .symbolize_keys if ['production', 'staging'].include? Rails.env @connection = Mongo::ReplSetConnection.new([:replicaset], :read => :secondary, :name => [:replicaset_name]) @connection.add_auth([:database], [:username], [:password]) @connection.add_auth('admin', [:username], [:password]) else url = "mongodb://#{[:host]}" @connection = Mongo::Connection.from_uri(url) end end |
Instance Attribute Details
#connection ⇒ Object (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
#all ⇒ Object
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') = {"updated_at" => {"$lt" => last}} = [['updated_at', 'descending']] elsif keys.include?('created_at') = {"created_at" => {"$lt" => last}} = [['created_at', 'descending']] else = {} end else = {} end query = coll.find().limit(25) if query.sort() 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 |