Mongoid创建空集合

时间:2015-05-22 03:25:57

标签: ruby-on-rails mongoid

我有剪切代码在MongoDB中创建空集合

session = Moped::Session.new([ '127.0.0.1:27017' ])
  database = Moped::Database.new(session, 'test_database')
  collection = Moped::Collection.new(database,'test_collection')

但它不起作用。我怎么能让它有效?感谢

我正在使用带有Mongoid的Ruby on Rails

1 个答案:

答案 0 :(得分:1)

尽管我已经知道Moped界面中没有任何内容像本地createCollection命令一样。当您尝试向其中插入数据或在其上创建索引时,Mongoid和Moped通常会创建集合。

但是,如果您愿意使用command方法,可以直接致电createCollection

database.command(:eval => 'db.createCollection("test_collection")')