emporio armani,fake rolex for sale,rolex sea dweller,panerai,iwc,richard mille,replica watches,bell ross,a lange sohne,cheap replica watches,jaeger lecoultre,rolex explorer,corum,rolex milgauss,breguet,piaget,franck muller,chopard
Archives for category: mongodb

I have a collection of users who store their favorite stuff id’s in an array, like this:

2.3-head :212 > User.each{ |u| puts u.inspect }
 => #<User _id: BSON::ObjectId('587356c45973747b77f1a35c'), favorite_ids: [1, 3, 5]>
 => #<User _id: BSON::ObjectId('587356c45973747b77f1a35d'), favorite_ids: [7, 3, 5, 2, 8]>
 => #<User _id: BSON::ObjectId('587356c45973747b77f1a35e'), favorite_ids: [3, 2, 7]>
 => #<User _id: BSON::ObjectId('587356c45973747b77f1a35f'), favorite_ids: [1, 10]>

I want to find those users who have favorited items 2, 3 and 8, and get them sorted by number of matchings.

class User
  def self.find_matches(array)
    User.collection.aggregate([
      { "$match" => { favorite_ids: { "$in" => array } } },
      { "$unwind" => "$favorite_ids" },
      { "$match" => { favorite_ids: { "$in" => array } } },
      { "$group" => { _id: "$_id", match_count: { "$sum" =>1 } } },
      { "$sort" => { match_count: -1 } }
    ])
  end
end

Finally:

2.3-head :228 >   User.find_matches([2,3,8]).entries
 => [{"_id"=>BSON::ObjectId('587356c45973747b77f1a35d'), "match_count"=>3}, {"_id"=>BSON::ObjectId('587356c45973747b77f1a35e'), "match_count"=>2}, {"_id"=>BSON::ObjectId('587356c45973747b77f1a35c'), "match_count"=>1}]

After installing all 4 Ubuntu VMs, then installing 10gen’s Mongodb package, then I went to restore my dump inside the replicaset. For my surprise:

root@mongo0:~# mongorestore --db my_database dump/my_database/
connected to: 127.0.0.1
terminate called after throwing an instance of 'std::runtime_error'
  what():  locale::facet::_S_create_c_locale name not valid
Aborted (core dumped)

A lot of people around says you have to edit /etc/locale.gen, then regenerate locales using locale-gen. For me it didn’t work. But as the problem comes from locales, then the solution was easy:

root@mongo0:~# env LC_ALL=en_US.UTF-8 mongorestore --db my_database dump/my_database/

That’s it.