I am working within a Rails app that uses MongoDB and Mongoid. I can run mongo queries using the mongo shell environment, but I'd love to play with Mongoid in irb. Is that something that I can do? If so, could someone please tell me how?
Thanks :)
I am working within a Rails app that uses MongoDB and Mongoid. I can run mongo queries using the mongo shell environment, but I'd love to play with Mongoid in irb. Is that something that I can do? If so, could someone please tell me how?
Thanks :)
As mentioned by Semyon the easiest option is to use:
$ rails console
If you want to do it manually, run up irb and prepend it with the environment you want (which is most likely development).
e.g./
$ RACK_ENV=development irb
Then require the mongoid gem & load your mongoid config & you should be able to use Mongoid. You'll also need to require any models you wish to use.
e.g/
> require 'mongoid'
> Mongoid.load!("path/to/your/mongoid.yml")
BTW. I'd recommend using pry rather than irb, it's like irb but you can find things out without leaving your terminal amongst other things.
e.g./
running
> ls Mongoid
shows me all the possible constants, Class & Instance Methods for Mongoid
constants: Atomic Attributes Callbacks Collection Collections Components Config Contexts Copyable Criteria Criterion Cursor DefaultScope Dirty Document Errors Extensions Extras Factory Fields Finders Hierarchy Identity IdentityMap Indexes Inspection Javascript JSON Keys Logger Matchers MONGODB_VERSION MultiDatabase MultiParameterAttributes NamedScope NestedAttributes Observer Paranoia Persistence Relations Reloading Safety Scope Serialization Sharding State Threaded Timestamps Validations VERSION Versioning
Mongoid#methods: add_language add_observer allow_dynamic_fields allow_dynamic_fields= allow_dynamic_fields? autocreate_indexes autocreate_indexes= autocreate_indexes? blacklisted_options config configure count_observers database database= databases databases= default_logger destructive_fields from_hash identity_map_enabled identity_map_enabled= identity_map_enabled? include_root_in_json include_root_in_json= include_root_in_json? include_type_for_serialization include_type_for_serialization= include_type_for_serialization? instantiate_observers load! logger logger= master master= max_retries_on_connection_failure max_retries_on_connection_failure= max_retries_on_connection_failure? notify_observers observer_instances observers observers= parameterize_keys parameterize_keys= parameterize_keys? persist_in_safe_mode persist_in_safe_mode= persist_in_safe_mode? preload_models preload_models= preload_models? purge! raise_not_found_error raise_not_found_error= raise_not_found_error? reconnect! scope_overwrite_exception scope_overwrite_exception= scope_overwrite_exception? skip_version_check skip_version_check= skip_version_check? time_zone time_zone= time_zone? unit_of_work use_activesupport_time_zone use_activesupport_time_zone= use_activesupport_time_zone? use_utc use_utc= use_utc?
Mongoid#methods: add_language add_observer allow_dynamic_fields allow_dynamic_fields= allow_dynamic_fields? autocreate_indexes autocreate_indexes= autocreate_indexes? blacklisted_options config configure count_observers database database= databases databases= default_logger destructive_fields from_hash identity_map_enabled identity_map_enabled= identity_map_enabled? include_root_in_json include_root_in_json= include_root_in_json? include_type_for_serialization include_type_for_serialization= include_type_for_serialization? instantiate_observers load! logger logger= master master= max_retries_on_connection_failure max_retries_on_connection_failure= max_retries_on_connection_failure? notify_observers observer_instances observers observers= parameterize_keys parameterize_keys= parameterize_keys? persist_in_safe_mode persist_in_safe_mode= persist_in_safe_mode? preload_models preload_models= preload_models? purge! raise_not_found_error raise_not_found_error= raise_not_found_error? reconnect! scope_overwrite_exception scope_overwrite_exception= scope_overwrite_exception? skip_version_check skip_version_check= skip_version_check? time_zone time_zone= time_zone? unit_of_work use_activesupport_time_zone use_activesupport_time_zone= use_activesupport_time_zone? use_utc use_utc= use_utc?
You can also start a preconfigured Rails console in your project:
$ rails console
>> MyDocument.where(:foo => 'bar').to_a
=> [...]