索引 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});
在 ArangoDB 2.x 中,Foxx 提供了分别称为存储库和模型的集合和文档(即存储在这些集合中的数据集)的包装器。每个存储库代表一个集合,每个模型代表一个文档。 ArangoDB 3.0 将提供一个新的、简化的 API,通过鼓励您使用 ArangoDB 已经提供的底层集合 API 来消除这种额外的复杂性。
为了在 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
});
当存储库被实例化(即调用new MyRepo(/* ... */))时,Foxx 将确保根据需要创建索引。
请参阅https://docs.arangodb.com/Foxx/Develop/Repository.html#defining-indexes 上的文档。
或者,如果您不想使用 Foxx 存储库,您可以在创建集合后使用上面的常规索引 API 在设置脚本中简单地定义索引。无论哪种方式,您都无需担心多次运行代码:如果索引已经存在,ensureIndex 将不会执行任何操作。