简体   繁体   English

如何在ArangoDB FOXX中定义索引?

[英]How to define an index in ArangoDB FOXX?

Can a FOXX application automatically create an index? FOXX应用程序可以自动创建索引吗? I have a collection (model) where I need a field to be used as a unique index for performance. 我有一个集合(模型),我需要一个字段作为性能的唯一索引。 I could create the hash after the fact, but I just wanted to be sure it wasn't available using the model definition. 我可以在事后创建哈希,但我只是想确保使用模型定义不可用。 If so, where can I find documentation? 如果是这样,我在哪里可以找到文档?

A secondary question is how to create an index in FOXX? 第二个问题是如何在FOXX中创建索引? I know how to do it in arangojs but I can't seem to find it in the FOXX documentation. 我知道如何在arangojs中做到这一点,但我似乎无法在FOXX文档中找到它。 Scratch this question. 抓住这个问题。 I figured this out: db.collection.createIndex(). 我想出来了:db.collection.createIndex()。 But boy was that hidden deep in the "misc" section of the documentation. 但男孩深藏在文档的“misc”部分。

The index API is not part of the Foxx API but the general ArangoDB API (Foxx is merely the framework ArangoDB provides for building and managing microservices) and can be found in the ArangoDB documentation: https://docs.arangodb.com/IndexHandling/WorkingWithIndexes.html 索引API不是Foxx API的一部分,但是一般的ArangoDB API(Foxx只是ArangoDB为构建和管理微服务提供的框架),可以在ArangoDB文档中找到: https ://docs.arangodb.com/IndexHandling/ WorkingWithIndexes.html

'use strict';
var myCollection = applicationContext.collection('my-data');
myCollection.ensureIndex({type: 'hash', fields: ['a', 'b'], unique: true});

In ArangoDB 2.x Foxx provides wrappers around collections and documents (ie datasets stored in those collections) called repositories and models respectively. 在ArangoDB 2.x中,Foxx提供了分别称为存储库和模型的集合和文档(即存储在这些集合中的数据集)的包装器。 Each repository represents a collection and each model represents a document. 每个存储库代表一个集合,每个模型代表一个文档。 ArangoDB 3.0 will provide a new, simplified API that gets rid of this additional complexity by encouraging you to use the underlying collection APIs ArangoDB already provides. ArangoDB 3.0将提供一个新的简化API,通过鼓励您使用ArangoDB已经提供的底层集合API来消除这种额外的复杂性。

In order to use the index-specific methods on Foxx repositories (like the geo queries for collections with geo indexes) you need to define the repository with the additional indexes property like so: 为了在Foxx存储库上使用特定于索引的方法(比如使用地理索引的集合的地理查询),您需要使用其他indexes属性来定义存储库,如下所示:

'use strict';
var Foxx = require('org/arangodb/foxx').Repository;
var MyModel = Foxx.Model.extend({/* ... */});
var MyRepo = Foxx.Repository.extend({
  indexes: [
    // same syntax as collection.ensureIndex:
    {type: 'hash', fields: ['a', 'b'], unique: true}
  ]
});
var repo = new MyRepo(applicationContext.collection('my-data'), {
  model: MyModel
});

When the repository is instantiated (ie new MyRepo(/* ... */) is invoked), Foxx will ensure the indexes are created as necessary. 在实例化存储库时(即调用new MyRepo(/* ... */) ),Foxx将确保根据需要创建索引。

See the documentation at https://docs.arangodb.com/Foxx/Develop/Repository.html#defining-indexes . 请参阅https://docs.arangodb.com/Foxx/Develop/Repository.html#defining-indexes上的文档。

Alternatively if you don't want to use Foxx repositories you can simply define the indexes in your setup script after creating the collection, using the regular index API above. 或者,如果您不想使用Foxx存储库,则可以使用上面的常规索引API在创建集合后在设置脚本中定义索引。 Either way you don't need to worry about running the code multiple times: ensureIndex will do nothing if the index already exists. 无论哪种方式,您都不必担心多次运行代码:如果索引已经存在, ensureIndex将不执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM