本文介绍了用蒙古文动态创建索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一份为文档创建新字段的工作,我希望在此工作结束时为该字段创建索引.我尝试过

I have a job that create new fields to my document, I want, at the end of this job, to create indexes to this fields. I tried

Model.index("field"=>-1)

还有

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

没有成功

这可能吗?

推荐答案

Model.index(:field => -1) 或多或少只是在Model中注册索引的存在,而实际上并没有创建索引.您正在寻找 create_indexes :

Saying Model.index(:field => -1), more or less, just registers the existence of the index with Model, it doesn't actually create an index. You're looking for create_indexes:

将实际的索引创建注释发送到MongoDB驱动程序

Send the actual index creation comments to the MongoDB driver

所以你想说:

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

您还可以通过调用 create 在集合的 indexes :

You can also create them directly through Moped by calling create on the collection's indexes:

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

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

Mongoid::Sessions has been renamed to Mongoid::Clients in newer versions so you might need to say:

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

感谢 js_ 注意此更改.

这篇关于用蒙古文动态创建索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 14:00