Simple example of using MongoMapper with Ruby

The MongoMapper web site is really lacking on even simple examples, especially those that don’t use Rails. So, here’s 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 "user created"
user.save!
end

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

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

user.destroy
end
end


class User
include MongoMapper::Document
key :name, String
timestamps!
end

app = App.new()
app.create_user
app.find_user
app.delete_user

The code does a few things:

  1. Creates a new user with a single field called name.
  2. Finds the user using the where function
  3. Removes (destroys/deletes) the user

The key things to note are that the where 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.

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

If there were no results, the result of calling first in this example would be that the user variable would be nil.

The delete_user function above has absolutely no error checking.

Blue Iris 3 and Samsung SmartCam SNH-1011N

If you’ve got a Samsung SmartCam SNH-1011N and you want to use it with Blue Iris 3, I found a reasonable currently undocumented way to make it work. The camera currently isn’t officially supported by Blue Iris, but it can be made to work with a few simple steps. I’ve sent the following information to the Blue Iris developer and ideally it will become part of the standard application.

If you’re not familiar with Blue Iris – it’s an extremely capable “DVR” for many of your IP based web cameras that you may own. I’d highly recommend it. It does not however work with “cloud-only” style IP cameras, like DropCam.

After setting up the camera normally, and updating the firmware to the latest version (at the time of this, it is 1.07_130516), Add a new camera:

image

(I’ve just right-clicked on the Camera’s display).

Then, fill out the name and the other important fields on the General tab:

image

Click the Video tab, and select Network IP and then click the Configure… button.

image

First, select the camera. As this camera isn’t currently officially supported, you’ll need to select the protocol and stream type instead:

image

Select RTSP H.264/MJPG/MPEG4 from the list. It’s generally very near the top of the list. Next, type in the host name or IP address of the camera. It’s whatever you used to set up the camera originally.

image

When you setup the SmartCam, you created a Private Key.

imageHere’s the strange part, type only the first eight (8) characters of the Private Key into the password field:

image

Yes, just the first eight characters. If you use any more than eight, the connection will fail. The camera only uses the first eight.

Finally, in the Video / Path field, type: /profile5/media.smp as shown below:

image

Hit the OK button, do any other setup you’d like, and the camera should be working!

You might want to frame limit (as I was tracing the network traffic, I saw this was the recommended size that Blue Iris switches to automatically):

image