
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Documentation: http://alexeypetrushin.github.com/mongodb
Persistence for any Ruby Object & Driver enhancements for MongoDB.
Lower layers are independent from upper, use only what You need.
MongoDB itself is very powerful, flexible and simple tool, but the API of the Ruby driver is a little complicated. These enhancements alter the driver's API and made it more simple and intuitive.
# Requiring driver enhancements.
require 'mongo/driver'
# Changing some defaults (optional, don't do it if You don't need it).
#
# By default they are set to false to provide maximum performance, but if You use MongoDB as
# major application database (and not only for logging, andalytics and other minor tasks) it's
# usually better to set it to true.
Mongo.defaults.merge! multi: true, safe: true
# Connecting to test database and cleaning it before starting the sample.
connection = Mongo::Connection.new
db = connection.default_test
db.drop
# Collection shortcuts, access collection directly by typing its name,
# instead of `db.collection('some_collection')`.
db.some_collection
# Let's create two Heroes.
db.units.save name: 'Zeratul'
db.units.save name: 'Tassadar'
# Querying first and all documents matching criteria (there's
# also `:each` method, the same as `:all`).
p db.units.first(name: 'Zeratul') # => zeratul
p db.units.all(name: 'Zeratul') # => [zeratul]
db.units.all name: 'Zeratul' do |unit|
p unit # => zeratul
end
# Dynamic finders, handy way to do simple queries.
p db.units.by_name('Zeratul') # => zeratul
p db.units.first_by_name('Zeratul') # => zeratul
p db.units.all_by_name('Zeratul') # => [zeratul]
# Bang versions, will raise error if nothing found.
p db.units.first!(name: 'Zeratul') # => zeratul
p db.units.by_name!('Zeratul') # => zeratul
# Query sugar, use `:_gt` instead of `:$gt`. It's more convinient to use new hash
# syntax `{name: {_gt: 'Z'}}` instead of hashrockets `{name: {:$gt => 'Z'}}`.
Mongo.defaults[:convert_underscore_to_dollar] = true
p db.units.all(name: {_gt: 'Z'}) # => [zeratul]
Source: docs/driver.rb
More docs - there's no need for more docs, the whole point of this extension is to be small, intuitive, 100% compatible with the official driver, and require no extra knowledge. So, please use standard Ruby driver documentation.
Define migration steps, specify desired version and apply it (usually all this should be done via Rake task).
# Requiring support for migration.
require 'mongo/migration'
# Defining first migration, creating Zeratul (and removing it in rollback).
Mongo.migration 1 do |m|
m.up {|db| db.units.save name: 'Zeratul'}
m.down{|db| db.units.remove name: 'Zeratul'}
end
# Defining second migration, creating Tassadar (and removing it in rollback).
Mongo.migration 2 do |m|
m.up {|db| db.units.save name: 'Tassadar'}
m.down{|db| db.units.remove name: 'Tassadar'}
end
# Connecting to test database and cleaning it before starting the sample.
connection = Mongo::Connection.new
db = connection.default_test
db.drop
# Assigning database to migration.
Mongo.migration.db = db
# Let's migrate to the first version and create mighty Zeratul.
Mongo.migration.update 1
p Mongo.migration.current_version # => 1
p db.units.all # => [Zeratul]
# Rolling it back.
Mongo.migration.update 0
p Mongo.migration.current_version # => 0
p db.units.all # => []
# Updating to the latest version (if there's no explicit version
# then the highest available version will be chosen).
Mongo.migration.update
p Mongo.migration.current_version # => 2
p db.units.all # => [Zeratul, Tassadar]
Source: docs/migration.rb
Save any Ruby object to MongoDB, as if it's a document. Objects can be any type, simple or composite with other objects / arrays / hashes inside.
Note: the :initialize method should allow to create object without arguments.
# Connecting to test database and cleaning it before starting the sample.
require 'mongo/object'
connection = Mongo::Connection.new
db = connection.default_test
db.drop
# Let's define Game Unit.
class Unit
# Including Mongo::Object.
include Mongo::Object
attr_reader :name, :stats
# We need the initializer to be used also without arguments.
def initialize name = nil, stats = nil
@name, @stats = name, stats
end
# Creating internal object containing stats of the Unit.
class Stats
include Mongo::Object
attr_accessor :attack, :life, :shield
def initialize attack = nil, life = nil, shield = nil
@attack, @life, @shield = attack, life, shield
end
end
end
# Let's create two Heroes.
#
# It uses the same Driver API, everything works the same way as with hashes.
zeratul = Unit.new('Zeratul', Unit::Stats.new(85, 300, 100))
tassadar = Unit.new('Tassadar', Unit::Stats.new(0, 80, 300))
db.units.save zeratul
db.units.save tassadar
# Udating, we made error - mistakenly set Tassadar's attack as zero, let's fix it.
tassadar.stats.attack = 20
db.units.save tassadar
# Querying first and all documents matching criteria (there's also `:each` method,
# the same as `:all`).
p db.units.first(name: 'Zeratul') # => zeratul
p db.units.all(name: 'Zeratul') # => [zeratul]
db.units.all name: 'Zeratul' do |unit|
p unit # => zeratul
end
# Dynamic finders, handy way to do simple queries.
p db.units.by_name('Zeratul') # => zeratul
p db.units.first_by_name('Zeratul') # => zeratul
p db.units.all_by_name('Zeratul') # => [zeratul]
# Bang versions, will raise error if nothing found.
p db.units.first!(name: 'Zeratul') # => zeratul
p db.units.by_name!('Zeratul') # => zeratul
# Query sugar, use `:_gt` instead of `:$gt`. It's more convinient to use new hash
# syntax `{name: {_gt: 'Z'}}` instead of hashrockets `{name: {:$gt => 'Z'}}`.
Mongo.defaults[:convert_underscore_to_dollar] = true
p db.units.all(name: {_gt: 'Z'}) # => [zeratul]
Source: docs/object.rb
gem install mongodb
Copyright (c) Alexey Petrushin, http://petrush.in, released under the MIT license.
FAQs
Unknown package
We found that mongodb demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.