可以通过命令行运行Mongoid查询吗?

时间:2012-07-04 20:20:25

标签: ruby-on-rails mongodb mongoid

我在使用MongoDB和Mongoid的Rails应用程序中工作。我可以使用mongo shell环境运行mongo查询,但我喜欢在irb中使用Mongoid。那是我能做的吗?如果是这样,有人可以告诉我怎么样?

谢谢:)

3 个答案:

答案 0 :(得分:4)

正如Semyon所提到的,最简单的选择是使用:

$ rails console

如果您想手动执行此操作,请运行irb并在其中添加您想要的环境(最有可能是开发)。

e.g ./

$ RACK_ENV=development irb

然后需要mongoid gem&加载您的mongoid配置&你应该能够使用Mongoid。您还需要要求使用任何型号。

e.g /

> require 'mongoid'

> Mongoid.load!("path/to/your/mongoid.yml")

顺便说一句。我建议使用pry而不是irb,它就像irb,但是你可以在不离开终端的情况下找到它们。

e.g ./

运行

> ls Mongoid

向我显示所有可能的常量,Class& 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?

答案 1 :(得分:3)

您还可以在项目中启动预配置的Rails控制台:

$ rails console
>> MyDocument.where(:foo => 'bar').to_a
=> [...]

答案 2 :(得分:2)

在Rails控制台中

db = Mongoid::Clients.default

collection = db[:collection_name]

现在我们可以对集合执行查询。

要查看所有可能的方法,请使用

collection.methods.sort!