Using $inc to increment a field in a sub-document in an array and a field in main document

(Blog post inspired by question I answered on StackOverflow)

Lets say you have a schema in MongoDB that looks something like this:

{
  '_id' : 'star_wars',
  'count' : 1234,
  'spellings' : [ 
    { spelling: 'Star wars', total: 10}, 
    { spelling: 'Star Wars', total : 15}, 
    { spelling: 'sTaR WaRs', total : 5} ]
}

Atomically, you’d like to update two fields at one time, the total for a particular spelling which is in a sub document in an array, and the overall count field.

The way to handle this is to take advantage of the positional array operator and the $inc operator.

Starting with just updating the count, that’s easy:

db.movies.update( 
    {_id: "star_wars"}, 
    { $inc : { 'count' : 1 }}
)

The query matches on the document with the _id of star_wars, and increments the count by 1.

The positional operator is where the mongo-magic comes into play here. If you wanted to just update a single sub-document in the array, add it to the query. First, we’ll try finding the right document:

db.movies.find( {
    _id: "star_wars", 
    'spellings.spelling' : "Star Wars" }
)

That matches the right document, but also returns all of the elements of the array.

But, wait, there’s more! When you match on an element/document of an array, the position of the match is remembered and can be used in the update phase. You do that using the positional operator. Using the document above, you’ll use this: spellings.$.total. If you knew the specific index into the array, the $ could have been replaced with the zero-based index number (like a 1 for example in this case: spellings.1.total).

Putting it all together then results in a slick and simple way of incrementing multiple fields in a document.

db.movies.update( 
    {_id: "star_wars",
     'spellings.spelling' : "Star Wars" },
    { $inc : 
        { 'spellings.$.total' : 1, 
        'count' : 1 }})

Results:

{
  '_id' : 'star_wars',
  'count' : 1235,
  'spellings' : [ 
    { spelling: 'Star wars', total: 10}, 
    { spelling: 'Star Wars', total : 16}, 
    { spelling: 'sTaR WaRs', total : 5} ]
}

RewriteCond for web based RSS readers

Now that Google Reader is gone, there are more than a handful of replacements popping up. In order to allow images to show in the web-based RSS readers, I needed to edit the .htaccess file for my web site to allow the images to show, as the default for my web site is to prevent hotlinking from most websites (other than wiredprairie.us).

I’ve tried to gather those that seemed popular, so I’m putting the list I’ve got here so that it might help others (and maybe some of you will have other suggestions):

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?wiredprairie.us [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://www.google.com/reader/m/view/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://cloud.feedly.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?feedly.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?pinterest.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?bloglines.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?feedly.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?netvibes.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?facebook.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?twitter.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?feedspot.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?digg.com/reader/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://reader.aol.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)newsblur.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)theoldreader.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)hivereader.com/.*$ [NC] RewriteRule \.(jpe?g?|gif|png|bmp|tiff?|pic|mp3|doc|xls|ico)$ - [F,NC,L]

Modern C++, iterators and loops compared to C#

It has been a while since I looked much at modern C++. I’ve started intentionally simple and was exploring some of the ways that a simple list can be iterated. I hate to say this, but I remember the days when the standard template library was not necessarily something that was the easiest to get working on Windows with a Microsoft C++ compiler in a simple, safe and reliable way. My, how times have changed for the better!

While the overall syntax of C++ lacks the refined and designed elegance of a C# application, it has come a very long way!

I’ve thrown together a simple sample. The code creates a list of strings, pushes (not add, arrgh!) strings onto the list, and then iterates through the list in three similar, but syntactically different methods below:

image

OK, there’s a few awesome things here that make C++ much more approachable these days:

  1. a decent string class! No more hunting for a library or header file that has most of the functionality you need. (there’s a wstring as well for wchar_t needs)
  2. All the common Data structure types like list, map, vector, etc… that you might need without writing them your self.
  3. With using, the code doesn’t need to refer to types with namespaces (much like C#). So, instead of std::list<std:string>, it’s just list<string>, which I consider far more readable at a glance.
  4. No weird casting or worrying about strings when values are passed to the push_back function. Again, it just works.
  5. auto – the var of C# is called auto in C++. If I hadn’t used auto in the first iterator, the code would have declared the type as list<string>::iterator. While not terrible, auto reads well, as it’s likely that I’ll not need the detailed declaration to understand what’s being iterated upon.
  6. Second option is just syntactical sugar as it internally uses a standard for loop like in the first example internally. But, you’ll note there are anonymous functions/lambdas! The square [] brackets is the capture clause of the lambda expression. Unlike C#, where the compiler automatically determines what outer scoped variables will be used by the inner lambda function, in C++, it’s necessary to explicitly declare what is required. While this is a bit annoying, there are times where this extra declaration might cause programmers to think twice about all of the variables that are required in the lambda expression. In this instance, there aren’t any variables needed, so it’s empty.
  7. The last example is the most concise, and maybe a little less friendly at first and that’s mostly due to the heritage of C++ and how to make code most efficient. It’s called a range-based for statement. First, the code is using auto again and the type is a string as it’s declaring the type used within the list. The & symbol remember is a reference and the const is indicating that the value will not be changed by the inner block. These two together ideally make it so the value is observed in-place, without any need for a copy.

While the range-based for statement isn’t quite as readable as C#:

image

I think you might agree that the C++ version is more than passable, and nearly friendly!

image

(Aside, there is a shortcut initializer list syntax that can be used with non-aggregate types in C++, so an int for example that would have gotten rid of the calls to push_back).

Nest thermostat savings?

I created a really brief survey that I hope those of you who own Nests will considering taking the minute or two to fill out (it’s only 5 simple questions). Lots of people report “massive” savings with their Nest thermostats. As prior owners of various programmable thermostats, and as someone who was careful about turning/up down the thermostat before owning a Nest, we really haven’t seen any difference in our heating/cooling bills since we bought our 3 thermostats in November 2011. It’s disappointing of course, but expected.

You’ll be able to see all of the results after you take the survey.

http://bit.ly/nestsave

Thanks!

Dripping Mansfield Outdoor Wall Hydrant

We’ve got 4 of these on our house:

http://www.prier.com/products/retired-products/500-series.html

(Apparently, they were Mansfield 500s, but the product line was acquired by Prier in 2007 and is now similar to the Prier 400 series).

When the water was turned on inside the house, and the water valve was open at the hydrant, yet an attachment was not drawing water (for example, a connected hose with an end that was not open), a consistent leak occurred around the handle (some refer to it as leaking behind the handle). This meant that we couldn’t connect a water sprinkle on a timer without wasting lots of water (as the hydrant was always on).

We had two professional plumbers tell us that they’d need to tear open the finished walls on the inside of the house and replace the whole unit to fix the problem. Not believing these claims, I went in search of a solution.

One solution was to tighten the handle and was easy to perform. While that helped a bit, it wasn’t the solution we needed.

The best solution was to buy and use this kit:

http://www.amazon.com/Prier-Products-630-7755-Service-Hydrants/dp/B000LNPEIO/ref=pd_sxp_grid_pt_0_1

Prier Products 630-7755 Service Parts Kit

It doesn’t come with instructions on the package, instead you’re direct to watch a video on YouTube with the details.

I watched the video once through and then fixed 2 of our faucets in about 15 minutes. It would have taken about 10 minutes if I had brought the correct tools with me to the faucets.

I’m no plumber, but it was easy to do the steps.

Neither faucets drip anymore. (I haven’t fixed the others yet). Hope this helps someone who’s a doubter about this working. The kit is around $10, which is far less than your plumber would charge for the trip and work.