ruby – 使用mongoid动态创建索引

前端之家收集整理的这篇文章主要介绍了ruby – 使用mongoid动态创建索引前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个工作,为我的文档创建新的字段,我希望,在这个工作结束时,为这个字段创建索引.
我试过了
Model.index("field"=>-1)

并且

Mongoid::Sessions.default[:rating_prediction].ensureIndex

没有成功

这可能吗?

解决方法

Model.index(:field => -1),或多或少,只是用Model注册索引的存在,它实际上并不创建索引.你在找 create_indexes: @H_502_17@

- (true) create_indexes

Send the actual index creation comments to the MongoDB driver

所以你想说:

Model.index(:field => -1)
Model.create_indexes

您也可以通过在集合indexes调用create直接通过Moped创建它们:

Mongoid::Sessions.default[:models].indexes.create(:field => 1)
Model.collection.indexes.create(:field => 1)

Mongoid :: Sessions已在较新版本中重命名为Mongoid :: Clients,因此您可能需要说:

Mongoid::Clients.default[:models].indexes.create(:field => 1)
Model.collection.indexes.create(:field => 1)

感谢js_注意到这一变化.

原文链接:https://www.f2er.com/ruby/265469.html

猜你在找的Ruby相关文章