Ruby on Rails为MongoDb添加了新的集合

时间:2015-05-21 04:11:11

标签: ruby-on-rails mongodb

我编写了一个方法来将新集合添加到MongoDB中的现有数据库“test_database”中,但它无法正常工作。我在http://docs.mongodb.org/ecosystem/drivers/ruby/使用MongoDB驱动程序版本2.0.4,Ruby 1.9和Rails 3.2

def add_mongo_collections()
begin
  client = Mongo::Client.new([ '127.0.0.1:27017' ])
  database = Mongo::Database.new(client, 'test_database')
  collection = Mongo::Collection.new(database, 'test_collection')
  collection.create      
rescue Exception => e
  p "cannot add collection"      
end

调用方法collection.create后,发生异常

No server is available matching preference: #<Mongo::ServerSelector::Primary:0x000000076adef0 @tag_sets=[], @options={:database=>"admin"}, @server_selection_timeout=30>

任何人都可以帮助我吗?谢谢!

0 个答案:

没有答案