GithubHelp home page GithubHelp logo

mongo_hash's Introduction

Mongo Hash

MongoHash is a simple schema-less persistence layer for easily saving and retrieving documents from a mongodb database. Once a document has been created or retrieved, it can be dealt with in most ways as a normal ruby hash, and then easily saved back to the database.

Installation

gem install mongo_hash

Usage

Basic usage is simple:

require 'mongo'
require 'mongo_hash'
db = Mongo::MongoClient.new('localhost').db('mongo_hash_test', :safe => true)
@collection = db.collection('test')
mh = MongoHash.new(@collection)

Build up a hash and save it

mh = MongoHash.new(@collection)
mh['testkey'] = 1
mh['somekeys'] = {"podsix" => "jerks", "stimutacs" => "onemore"}
mh.save

Find all:

> MongoHash.find(@collection)
=> [{"testkey"=>1, "somekeys"=>{"podsix"=>"jerks", "stimutacs"=>"onemore"}, "created_at"=>1354035712}]

Find specific:

> MongoHash.find(@collection, {"testkey" => 1})
=> [{"testkey"=>1, "somekeys"=>{"podsix"=>"jerks", "stimutacs"=>"onemore"}, "created_at"=>1354035712}]

Find or create:

> mh = MongoHash.find(@collection, {"testkey" => 1}).first
=> {"testkey"=>1, "somekeys"=>{"podsix"=>"jerks", "stimutacs"=>"onemore"}, "created_at"=>1354035712} 
> mh = MongoHash.find(@collection, {"testkey" => 0}).first
=> nil

Add or delete keys:

> mh = MongoHash.find(@collection, {"testkey" => 1}).first
=> {"testkey"=>1, "somekeys"=>{"podsix"=>"jerks", "stimutacs"=>"onemore"}, "created_at"=>1354035712} 
> mh['somemorekeys'] = {"justthefacts" => "please"}
=> {"justthefacts"=>"please"} 
> mh['evenmorekeys'] = {"petshopboys" => "please"}
=> {"petshopboys"=>"please"} 
> mh.delete('somekeys')
=> {"podsix"=>"jerks", "stimutacs"=>"onemore"} 
> mh
=> {"testkey"=>1, "created_at"=>1354035712, "somemorekeys"=>{"justthefacts"=>"please"}, "evenmorekeys"=>{"petshopboys"=>"please"}} 
> mh.save
=> {"updatedExisting"=>true, "n"=>1, "connectionId"=>69, "err"=>nil, "ok"=>1.0}

Retrieve and update just a subkey hash (does not touch the rest of the record):

> mh = MongoHash.find(@collection, {"testkey" => 1}, "somemorekeys").first
=> {"somemorekeys"=>{"justthefacts"=>"please"}} 
> mh['somemorekeys']['peas'] = "please"
=> "please" 
> mh
=> {"somemorekeys"=>{"justthefacts"=>"please", "peas"=>"please"}}
=> {"updatedExisting"=>true, "n"=>1, "connectionId"=>75, "err"=>nil, "ok"=>1.0}
> mh = MongoHash.find(@collection, {"testkey" => 1}).first
=> {"created_at"=>1354036563, "evenmorekeys"=>{"petshopboys"=>"please"}, "somemorekeys"=>{"justthefacts"=>"please", "peas"=>"please"}, "testkey"=>1}

Ruby Version

MongoHash has been tested with ruby 1.9.3 and ree 1.8.7.

Contributors

mongo_hash's People

Contributors

fields avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.