如何解决在非Rails环境中通过Mongoid创建MongoDB索引
我正在尝试使用MongoDB作为我的数据库来构建基于Roda的(非Rails)API。我安装了Mongoid gem并配置了连接。不幸的是,当我尝试使用rake db:mongoid:create_indexes
创建定义的索引时,由于缺少environment
,该任务失败了。我应该怎么做/包括在Rakefile
中以提供所需的任务?
回溯:
rake aborted!
Don't know how to build task 'environment' (See the list of available tasks with `rake --tasks`)
/home/tomasz/.rvm/gems/ruby-2.7.1/bin/ruby_executable_hooks:24:in `eval'
/home/tomasz/.rvm/gems/ruby-2.7.1/bin/ruby_executable_hooks:24:in `<main>'
Tasks: TOP => db:mongoid:create_indexes
(See full trace by running task with --trace)
我的Rakefile
的相关部分:
require 'mongoid'
path = Gem::Specification.find_by_name('mongoid')
load "#{path.gem_dir}/lib/mongoid/railties/database.rake"
rake -T
输出:
rake db:create_indexes # Create indexes specified in Mongoid models
rake db:drop # Drops all the collections for the database for the current Rails.env
rake db:mongoid:create_indexes # Create indexes specified in Mongoid models
rake db:mongoid:drop # Drop the database of the default Mongoid client
rake db:mongoid:purge # Drop all non-system collections
rake db:mongoid:remove_indexes # Remove indexes specified in Mongoid models
rake db:mongoid:remove_undefined_indexes # Remove indexes that exist in the database but are not specified in Mongoid models
rake db:mongoid:shard_collections # Shard collections with shard keys specified in Mongoid models
rake db:purge # Drop all collections except the system collections
rake db:remove_indexes # Remove indexes specified in Mongoid models
rake db:reset # Delete data and loads the seeds
rake db:seed # Load the seed data from db/seeds.rb
rake db:setup # Create the database,and initialize with the seed data
rake db:shard_collections # Shard collections with shard keys specified in Mongoid models
解决方法
您需要:
- 定义一个名为
environment
的任务, - 在此任务中,加载所有模型类。
如何加载模型类取决于您的应用程序。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。