我正在使用带有 CouchDB 的 Hyperledger Fabric(版本 2.2.0,Hyperledger/fabric-couchdb docker 映像附带的版本)。Fabric 有一个限制,它不允许为 mango 查询指定排序数组,因此必须使用索引来完成。
我面临的问题是,无论我为索引字段指定排序“asc”还是“desc”,查询结果总是以相同的顺序出现。
我试图创建的索引(我也宁愿使用assetType作为部分索引选择器,但也没有成功):
{
"index": {
"fields": [
{"assetType": "desc"},
{"originalCustomer.document":"desc"},
{"transactionDate":"desc"}
]
},
"ddoc": "date-index",
"name": "date-index",
"type": "json"
}
查询我正在运行
{
"selector": {
"assetType": "receivable",
"originalCustomer.document": "1",
"transactionDate": {
"$gt":"1900-01-01"
}
},
"use_index": ["date-index"]
}
_解释结果
{
"dbname": "testdb",
"index": {
"ddoc": "_design/date-index",
"name": "date-index",
"type": "json",
"def": {
"fields": [{"assetType": "asc"},{"originalCustomer.document":"asc"},{"transactionDate": "asc"}]
}
},
"selector": {
"$and": [
{"assetType": {"$eq": "receivable"}},
{"originalCustomer.document": {"$eq": "1"}},
{"transactionDate": {"$gt": "1900-01-01"}}
]
},
"opts": {
...
},
"limit": 25,
"skip": 0,
"fields": "all_fields",
"mrargs": {
"include_docs": true,
"view_type": "map",
"reduce": false,
"start_key": [
"receivable",
"1",
"1900-01-01"
],
"end_key": [
"receivable",
"1",
"<MAX>"
],
"direction": "fwd",
"stable": false,
"update": true,
"conflicts": "undefined"
}
}
无论我在索引上使用“asc”还是“desc”,都会产生相同的 _find 结果。鉴于降序排列,我希望 transactionDate“2019-01-02”成为列表中的第一个(为简洁起见,我删除了不相关的字段)
{
"docs": [
{
"assetType": "receivable",
"originalCustomer": {"document": "1"},
"transactionDate": "2019-01-01"
},
{
"assetType": "receivable",
"originalCustomer": {"document": "1"},
"transactionDate": "2019-01-01"
},
{
"assetType": "receivable",
"originalCustomer": {"document": "1"},
"transactionDate": "2019-01-01"
},
{
"assetType": "receivable",
"originalCustomer": {"document": "1"},
"transactionDate": "2019-01-02"
},
{
"assetType": "receivable",
"originalCustomer": {"document": "1"},
"transactionDate": "2019-01-02"
}
],
"bookmark": "..."
}