Moongoon::Database
Used to connect to a MongoDB database instance.
Instance methods
Pass a block that will get executed after the database has been successfully connected and after the scripts are run.
Moongoon::Database.after_connect {
# ... #
}
Pass a block that will get executed after the database has been successfully connected but before the scripts are run.
Moongoon::Database.after_connect_before_scripts {
# ... #
}
Pass a block that will get executed before the server tries to connect to the database.
Moongoon::Database.before_connect {
puts "Before connecting…"
}
Connects to MongoDB.
Use an instance of Mongo::Options as the options argument to customize the Mongo::Client instance.
Will retry up to max_retries times to connect to the database. If max_retries is nil, will retry infinitely.
Will sleep for reconnection_delay between attempts.
# Arguments are all optional, their default values are the ones defined below:
Moongoon.connect("mongodb://localhost:27017", "database", options = nil, max_retries: nil, reconnection_delay: 5.seconds)
Acquires a database lock and yields the client and database objects.
Will acquire a lock named lock_name, polling the DB every delay to check the lock status. If abort_if_locked is true the block will not be executed and this method will return if the lock is acquired already.
# If another connection uses the "query" lock, it will wait
# until this block has completed before perfoming its own work.
Moongoon.connection_with_lock "query" { |client, db|
collection = db["some_collection"]
data = collection.find query
pp data
}