将MongoCollection放到Rails控制台中

问题描述

尝试将新数据插入我的一个Mongo集合(test_users)中时遇到错误

Errors: 'Mongo::Error::BulkWriteError: E11000 duplicate key error collection: test_users index: iden_1 (11000)

test_user.rb

class TestUser
  include Mongoid::Document
  include Mongoid::Timestamps

  store_in collection:  "test_users"

  field :iden,type: String
  field :group_num,type: String
  index({ iden: 1 },{ unique: true,background: true })
  
end

在线研究之后,最好的解决方案是删除集合以重置索引。除了使用mongo shell外,还有什么方法可以将集合删除到我的应用程序中?

例如,我可以从集合中删除所有文档

 TestUser.delete_all;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)