{"id":1965,"date":"2013-10-24T20:39:08","date_gmt":"2013-10-25T01:39:08","guid":{"rendered":"http:\/\/www.wiredprairie.us\/blog\/?p=1965"},"modified":"2013-10-24T20:40:23","modified_gmt":"2013-10-25T01:40:23","slug":"simple-example-of-using-mongomapper-with-ruby","status":"publish","type":"post","link":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1965","title":{"rendered":"Simple example of using MongoMapper with Ruby"},"content":{"rendered":"

The MongoMapper<\/a> web site is really lacking on even simple examples, especially those that don\u2019t use Rails. So, here\u2019s a simple example that might help someone. <\/p>\n

From the Gemfile:<\/p>\n

\n
source 'https:\/\/rubygems.org'<\/span>

gem 'mongo_mapper'<\/span>
<\/pre>\n

<\/div>\n

And then the application:<\/p>\n

\n
require 'mongo_mapper'<\/span>


# jam it into<\/span> the database<\/span> "mm"
<\/font>MongoMapper.database<\/span> = "mm"

class<\/span> App

def create_user
user<\/span> = User<\/span>.new<\/span>(:name => 'Johnny'<\/span>)
puts "user<\/span> created"
user<\/span>.save<\/span>!
end<\/span>

def find_user
query = User<\/span>.where<\/span>(:name => 'Johnny'<\/span>)
user<\/span> = query.first<\/span> # just the first<\/span>
if<\/span> not<\/span> user<\/span>.nil?
puts user<\/span>.id
end<\/span>
end<\/span>

def delete_user
query = User<\/span>.where<\/span>(:name => 'Johnny'<\/span>)
user<\/span> = query.first<\/span> # just the first<\/span>

user<\/span>.destroy<\/span>
end<\/span>
end<\/span>


class<\/span> User<\/span>
include<\/span> MongoMapper::Document
key<\/span> :name, String
timestamps!
end<\/span>

app = App.new<\/span>()
app.create_user
app.find_user
app.delete_user<\/pre>\n

<\/div>\n

The code does a few things:<\/p>\n

    \n
  1. Creates a new user with a single field called name.<\/strong><\/li>\n
  2. Finds the user using the where<\/strong> function<\/li>\n
  3. Removes (destroys\/deletes) the user<\/li>\n<\/ol>\n

    The key things to note are that the where <\/strong>function returns a query and not the actual results. The results are fetched on demand. This is very similar to the extension methods and LINQ in .NET as those functions build a query that executed only when the results are first requested. <\/p>\n

    The same thing is true of MongoMapper in this case. The results are not returned until the first<\/strong> function is called. Alternatively, all<\/strong> or last <\/strong>could have been used. all<\/strong> of course returning a list of results that could be iterated in a loop.<\/p>\n

    If there were no results, the result of calling first<\/strong> in this example would be that the user<\/strong> variable would be nil. <\/strong><\/p>\n

    The delete_user <\/strong>function above has absolutely no error checking. <\/em><\/p>\n","protected":false},"excerpt":{"rendered":"

    The MongoMapper web site is really lacking on even simple examples, especially those that don\u2019t use Rails. So, here\u2019s a simple example that might help someone. From the Gemfile: source ‘https:\/\/rubygems.org’gem ‘mongo_mapper’ And then the application: require ‘mongo_mapper’# jam it into the database "mm"MongoMapper.database = "mm"class App def create_user user = User.new(:name => ‘Johnny’) puts […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[129,57],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pd5QIe-vH","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1754,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1754","url_meta":{"origin":1965,"position":0},"title":"Nest Thermostat API using Node JS and Nest API Update","date":"October 9, 2012","format":false,"excerpt":"I\u2019ve been asked by a few people for more details on the API Nest Labs uses for their thermostats, especially regarding setting data (and not just polling). The API uses mostly JSON formatted data POSTed to their web servers. Authentication To authenticate, POST the username and password, encoded as form\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1442,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1442","url_meta":{"origin":1965,"position":1},"title":"Nest Thermostat API\/Protocol","date":"January 8, 2012","format":false,"excerpt":"While Nest Labs hasn\u2019t released a formal (documented & supported) API, I thought I\u2019d do a bit of digging to see how they\u2019re using the network and what might be achievable. A few things are going on, the majority as you\u2019d probably expect. The web interface is using a long\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"image","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2012\/01\/image_thumb7.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1430,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1430","url_meta":{"origin":1965,"position":2},"title":"Ember.JS and EZdata, and Rails","date":"January 7, 2012","format":false,"excerpt":"I\u2019ve been trying to do some additional work on my ember.js extension for data management. At the same time though, I\u2019ve been trying (to learn and) build a simple Ruby on Rails web demo application using the new JavaScript library. There have been more than a few things that have\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1835,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1835","url_meta":{"origin":1965,"position":3},"title":"How to rewrite a MongoDB C# LINQ with a Projection Requirement using a MongoCursor","date":"January 26, 2013","format":false,"excerpt":"The LINQ Provider for MongoDB does not currently take into account data projections efficiently when returning data. This could mean that you\u2019re unnecessarily returning more data from the database than is needed. So, I\u2019m going to show you the pattern I applied as a replacement for the LINQ queries when\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":331,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/331","url_meta":{"origin":1965,"position":4},"title":"Velocity — a rockin’ distributed in memory cache for ASP.NET","date":"June 3, 2008","format":false,"excerpt":"Velocity, the code-name for a new in-memory distributed caching system for ASP.NET was released as a Community Tech Preview today. What is it? It's described in the documentation: Microsoft project code named \"Velocity\" provides a highly scalable in-memory application cache for all kinds of data. By using cache, your application\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"image","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2008\/06\/image-thumb.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":388,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/388","url_meta":{"origin":1965,"position":5},"title":"Silverlight Weather Demonstration","date":"June 26, 2008","format":false,"excerpt":"Demonstration available here. (You'll need to wait for a moment while it loads the first time). I've created a reasonably simple, yet multi-technology (and discipline) demonstration using Silverlight for the user interface and ASP.NET as the back-end. The demonstration uses: Silverlight 2.0 Data binding Delayed downloading of images \"Web services\"\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2008\/06\/image21.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/1965"}],"collection":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/comments?post=1965"}],"version-history":[{"count":2,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/1965\/revisions"}],"predecessor-version":[{"id":1967,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/1965\/revisions\/1967"}],"wp:attachment":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/media?parent=1965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/categories?post=1965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/tags?post=1965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}