Skip to content

Instantly share code, notes, and snippets.

@jcsalterego
Created December 28, 2009 17:38
Show Gist options
  • Select an option

  • Save jcsalterego/264797 to your computer and use it in GitHub Desktop.

Select an option

Save jcsalterego/264797 to your computer and use it in GitHub Desktop.
# clean start
>> words.drop
=> true
# unique index on 'ngram' and 'n'
>> words.create_index([[:ngram, 1], [:n, 1]], true)
=> "ngram_1_n_1"
# insert 'bar foo'
>> words.update({:n => 2, :ngram => 'bar foo'.split}, {'$inc' => {:freq => 1}}, {:upsert => true})
=> true
>> words.find.to_a
=> [{"_id"=>4b38ec70af79396fd14f8bbd, "ngram"=>["bar", "foo"], "n"=>2, "freq"=>1}]
# insert 'bar foo', such that freq => 2
>> words.update({:n => 2, :ngram => 'bar foo'.split}, {'$inc' => {:freq => 1}}, {:upsert => true})
=> true
>> words.find.to_a
=> [{"_id"=>4b38ec70af79396fd14f8bbd, "ngram"=>["bar", "foo"], "n"=>2, "freq"=>2}]
# insert 'foo bar', gets sucked into oblivion
>> words.update({:n => 2, :ngram => 'foo bar'.split}, {'$inc' => {:freq => 1}}, {:upsert => true})
=> true
>> words.find.to_a
=> [{"_id"=>4b38ec70af79396fd14f8bbd, "ngram"=>["bar", "foo"], "n"=>2, "freq"=>2}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment