Usually to remove some fields from collections in MongoDB, $unset is used. What can I do in Ruby to accomplish that? I have tried to read this, but I couldn't understand how to use it. I am new to Ruby and still learning to work with it.
The $unset operator is used through the update command which is documented here for the ruby driver.
Taking a step back, the standard (10gen-maintained) MongoDB Ruby driver can do absolutely everything that mongodb can. It exposes very literally the full capabilities of mongodb, just like the mongodb drivers in other languages do. So don't worry about being able to do mongo things in any particular language.
You might be wondering about the capabilities of popular ODM layers since they vary. ODM layers map mongodb documents to native language objects. In Ruby, mongoid is the most popular ODM. As #Tilo points out, it also supports explicitly removing fields from documents through its higher-level API.
Please check this issue for Mongoid:
https://github.com/mongoid/mongoid/pull/635
p = Product.first
p.raw_attributes.delete :foo
p.save
Related
We have a logging system, and erlang OTP server is writing logs in erlang term.
We also have Rails interface for internal users, and I want to provide a log analysis for them.
I have tried to find an erlang term parser, not erlang parser, written in ruby. but no luck yet.
erlang terms are simple; atom, tuple, list(including string), binary, and pid/ref
atom is like a symbol
tuple is like a hash
list is like an array
binary/pid/ref are like string
Anyone knows any existing erl-to-ruby parser?
Maybe this isn't quite what you're looking for, but you could check out BERT-RPC. It has serializers, clients, and servers for various languages, including Ruby (they are listed at the bottom of the page).
BERT is new, and it seems overkill to me, and I don't see code out-there for this purpose,
I made my own.
https://github.com/bighostkim/erl_to_ruby
This module from the people at basho seems to be exactly what you need.
https://github.com/basho/erlang_template_helper
I'm need to find a Ruby ORM (Active Record, Sequel, etc) that supports PostgreSQL's Array column datatype.
http://www.postgresql.org/docs/8.2/interactive/arrays.html
Any tips?
Sequel's sql_subscript is just for accessing values when querying, as you mentioned in an earlier comment.
Sequel doesn't have special support for database arrays other than that. You can create arrays in create_table blocks:
DB.create_table do
column :numbers, 'integer[]'
end
But that's not special support as Sequel is just passing the type through. There's no built-in support for taking a ruby array and turning it into an PostgreSQL array when building a query, and there's also no support for turning the PostgreSQL array into a ruby array when retrieving (it'll be returned as a string).
That being said, Sequel is set up so that extensions for PostgreSQL's array and hstore types that offer full integration could be implemented with relative ease. Someone has been working on support for hstore (I think it's finished or close to it, but I haven't reviewed it yet), and support for arrays should be similar to that. It's likely that a future version of Sequel will ship with such support either by default or available as an official extension.
Sequel supports this through the sql_subscript method on Symbols (and others).
Support was originally added in release 0.4.3 (2007-Dec) via Symbol#| and Symbol#/, but was changed to use the new method in release 2.12.0 (2009-Apr). Search the CHANGELOG for more mentions of improvements that have occurred over time.
I cannot seem to find any Object Databases for Ruby (do not confuse with Document Oriented, like Coach and Mongo).
Do more or less mature OODBMSs for Ruby exist?
Thanks.
I don't know much about it, but I believe that the MagLev implementation of Ruby will include an object database.
There aren't any mature ones — because it isn't typically done or desired by Rubyists.
In Ruby, there are currently 3 ODM (object data mappers) maintained:
MongoMapper
Mongoid
MongoDoc
Which is your preferred and why?
I just chose Mongoid for a new Rails 3 project based on the argument that it has the best documentation.
Since I am new to MongoDB (with many years of MySQL and a little bit of CouchDB experience), I needed good guides that take me by the hand and help me deliver something working within a reasonable span of time.
AFAIK Mongoid has just that: a nice looking site, with well documented guides for newbies like me.
Code Stats for Mongoid and MongoMapper
It looks like MongoMapper has much better code quality (if it does the same with less).
Here's the analyzer CodeStats https://github.com/alexeypetrushin/code_stats
In my opinion it's hard to say which is better, if you have DataMapper experience you'll like MongoMapper, but if you used ActiveRecord, Mongoid it's your preferred choice. I believe all of them worths a try concerning the context where you want to use them.
I am using MongoMapper. It good except it is little slow with Time conversions.
And it loads all data as Array. MyCollection.all for example gives you huge array, not cursor.
while Mongoid says: - Optimized for use with extremely large datasets.
So I guess you could try MongoID if you need speed and have big recordsets.
I was trying MongoMapper, but I think I'll go with Mongoid, because after quick reading docs it seems to me somewhat easier. Plus, it's developed by guys from Hashrocket, so that's a good reason itself.
While not a direct answer to your question, I would also consider using the basic ruby driver directly. Unlike the various SQL adapters out there Mongo's ruby class is easy to use and powerful. Because queries are hashes, composing queries is generally easy. The real advantage is access to the Atomic Modifiers. If you benefit from a Document database, these modifiers should be in your toolbox.
Having said this, I will go ahead and recommend MongoMapper because it has cleaner integration with the non-CRUD parts of MongoDB. Both projects are making gains in this area, and the situation may have changes since I did my research in December 2010.
I can recommend MongoMapper, since it also works with rails3 (beta and master). I personally didn't try the other 2 mappers you mentioned, since MM works great in my workflow and the mailinglist is very active. Furthermore the codebase is really stable and the only issue is with rails3 master, so you should use fredwu's branch, which already includes fixes for the current rails3 master changes on form_for: http://github.com/fredwu/mongomapper.git
There's also MongodbModel http://alexeypetrushin.github.com/mongodb_model
I'm considering writing a (large) desktop application in Ruby (actually a game, think something like Angband or Nethack using Gtk+ for the GUI). I'm coming from a C#/.NET background, so I'm a little at a lost for how to structure things.
In C#, I'd create many namespaces, like Application.Core, Application.Gui, etc). Parts of the application that didn't need the Gui wouldn't reference it with a using statement. From what I understand, in Ruby, the require statement basically does a textual insert that avoids duplicated code. What I'm concerned about, through the use of require statements, every file/class will have access everything else, because the ordering of the require statements.
I've read some ruby code that uses modules as namespaces. How does that work and how does it help?
Not sure what I'm getting at here... Does anyone have any good pointers on how to structure a large Ruby application? How about some non-trivial (and non-Rails) apps that use Ruby?
Ruby is no different from any other language when it comes to structuring your code. Do what feels right and it will probably work. I'm not entirely sure what problem you are anticipating. Are you worried about name clashes?
Modules are a good way to get pseudo namespaces, eg.
module Core
class Blah
self.def method
end
end
end
Core::Blah.method
Some of your problem isn't particular to Ruby, it's just about circular dependencies. Does Core depend on Gui or does Gui depend on Core? Or both?
A good way around some of this problem is with a very small "runner" component that depends on the core, the data access components, the gui, and so on, and ties these all together.