我正在使用 MongoDB 和 Mongoid 的 Rails 应用程序中工作。我可以使用 mongo shell 环境运行 mongo 查询,但我很想在 irb 中使用 Mongoid。那是我能做的吗?如果是这样,有人可以告诉我怎么做吗?
谢谢 :)
我正在使用 MongoDB 和 Mongoid 的 Rails 应用程序中工作。我可以使用 mongo shell 环境运行 mongo 查询,但我很想在 irb 中使用 Mongoid。那是我能做的吗?如果是这样,有人可以告诉我怎么做吗?
谢谢 :)
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?
您还可以在项目中启动预配置的 Rails 控制台:
$ rails console
>> MyDocument.where(:foo => 'bar').to_a
=> [...]
在 Rails 控制台中
db = Mongoid::Clients.default
collection = db[:collection_name]
现在我们可以对集合执行查询。
要查看所有可能的方法,请使用
collection.methods.sort!