Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 使用mongoid动态创建索引_Ruby_Mongodb_Mongoid - Fatal编程技术网

Ruby 使用mongoid动态创建索引

Ruby 使用mongoid动态创建索引,ruby,mongodb,mongoid,Ruby,Mongodb,Mongoid,我有一个为我的文档创建新字段的作业,我希望在该作业结束时为该字段创建索引。 我试过了 而且 Mongoid::Sessions.default[:rating_prediction].ensureIndex 无功而返 这可能吗?或多或少地说,只是用Model注册了索引的存在,实际上并没有创建索引。您正在寻找: -(true)创建索引 将实际的索引创建注释发送到MongoDB驱动程序 所以你想说: Model.index(field: -1) Model.create_indexes 您还可以

我有一个为我的文档创建新字段的作业,我希望在该作业结束时为该字段创建索引。 我试过了

而且

Mongoid::Sessions.default[:rating_prediction].ensureIndex
无功而返

这可能吗?

或多或少地说,只是用
Model
注册了索引的存在,实际上并没有创建索引。您正在寻找:

-(true)创建索引

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

所以你想说:

Model.index(field: -1)
Model.create_indexes
您还可以通过调用集合的以下命令,直接通过Moped创建它们:

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

Mongoid::Clients.default[:models].indexes.create(field: 1)
Model.collection.indexes.create(field: 1)
# or in even newer versions:
Model.collection.indexes.create_one(field: 1)

感谢并注意到这些更改。

作为将来的参考,
Mongoid::Sessions
现在被称为
Mongoid::Clients
,@js\uu>感谢您让我知道。该方法现在被称为
create\u one
,以便在Mongoid中动态创建唯一的复合索引:
Model.collection.index.create\u one({field:1,另一个{field:1},{unique:true})
@mltsy喜欢更新的版本吗?我不再使用MongoDB做任何事情,所以我很难检查。很抱歉回复太晚。我不确定它是什么时候更改的-据我所知,自从MongoDB 2.0以来,它一直是
创建一个
Mongoid::Sessions.default[:models].indexes.create(field: -1)
Model.collection.indexes.create(field: 1)
# or in newer versions:
Model.collection.indexes.create_one(field: 1)
Mongoid::Clients.default[:models].indexes.create(field: 1)
Model.collection.indexes.create(field: 1)
# or in even newer versions:
Model.collection.indexes.create_one(field: 1)