在非 Rails 环境中通过 Mongoid 创建 MongoDB 索引
Creating MongoDB indexes via Mongoid in non-Rails environment
我正在尝试使用 MongoDB 作为我的数据库来构建基于 Roda 的(非 Rails)API。我安装了 Mongoid gem 并配置了连接。不幸的是,当我尝试使用 rake db:mongoid:create_indexes
创建定义的索引时,由于缺少 environment
一个,任务失败了。我应该 do/include 在我的 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
、 的任务
- 在此任务中加载所有模型 类。
如何加载您的模型类取决于您的应用程序。
我正在尝试使用 MongoDB 作为我的数据库来构建基于 Roda 的(非 Rails)API。我安装了 Mongoid gem 并配置了连接。不幸的是,当我尝试使用 rake db:mongoid:create_indexes
创建定义的索引时,由于缺少 environment
一个,任务失败了。我应该 do/include 在我的 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
、 的任务
- 在此任务中加载所有模型 类。
如何加载您的模型类取决于您的应用程序。