【发布时间】:2012-07-05 07:25:02
【问题描述】:
我在使用 MongoDB 和 Mongoid 的 Rails 应用程序中工作。我可以使用 mongo shell 环境运行 mongo 查询,但我很想在 irb 中使用 Mongoid。那是我能做的吗?如果是这样,有人可以告诉我怎么做吗?
谢谢:)
【问题讨论】:
标签: ruby-on-rails mongodb mongoid
我在使用 MongoDB 和 Mongoid 的 Rails 应用程序中工作。我可以使用 mongo shell 环境运行 mongo 查询,但我很想在 irb 中使用 Mongoid。那是我能做的吗?如果是这样,有人可以告诉我怎么做吗?
谢谢:)
【问题讨论】:
标签: ruby-on-rails mongodb mongoid
正如 Semyon 所说,最简单的选择是使用:
$ rails console
如果您想手动执行此操作,请运行 irb 并在其前面加上您想要的环境(这很可能是开发)。
例如/
$ RACK_ENV=development irb
然后需要 mongoid gem 并加载你的 mongoid 配置 & 你应该能够使用 Mongoid。您还需要要求您希望使用的任何模型。
例如/
> require 'mongoid'
> Mongoid.load!("path/to/your/mongoid.yml")
顺便说一句。我建议使用pry 而不是 irb,它就像 irb 但你可以在不离开终端的情况下找到东西。
例如/
运行
> ls Mongoid
向我展示所有 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
=> [...]
【讨论】:
to_a 方法感到困惑?这是将对象转换为数组的常用 Ruby 方法。您通常可以在不显式调用 to_a 的情况下对其进行迭代。例如,范围就是这样工作的。如果您在 irb 中调用 1..9 ,您将看不到数组。但是你可以做(1..9).each{ |n| print n }。同样的事情也适用于 Mongoid Criteria(这是 Document.where 返回的对象类型)。您不会立即看到文档数组,但可以对其进行迭代或将其显式转换为数组。
在 Rails 控制台中
db = Mongoid::Clients.default
collection = db[:collection_name]
现在我们可以对集合执行查询了。
查看所有可能的方法使用
collection.methods.sort!
【讨论】: