I am using Hyperledger Fabric with CouchDB (version 2.2.0, the one that comes with the hyperledger/fabric-couchdb docker image). There is a limitation on Fabric which does not allow to specify sort array for mango queries, so it has to be done with indexes.
The problem I am facing is that no matter if I specify sort "asc" or "desc" fot the index fields, the query results always come in the same order.
The index I am trying to create (also I would rather use assetType as partial index selector, but had no success on that too):
{
"index": {
"fields": [
{"assetType": "desc"},
{"originalCustomer.document":"desc"},
{"transactionDate":"desc"}
]
},
"ddoc": "date-index",
"name": "date-index",
"type": "json"
}
Query I am running
{
"selector": {
"assetType": "receivable",
"originalCustomer.document": "1",
"transactionDate": {
"$gt":"1900-01-01"
}
},
"use_index": ["date-index"]
}
_explain result
{
"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"
}
}
the same _find result is produced no matter if I use "asc" or "desc" on the index. I am expecting transactionDate "2019-01-02" to be the first on the list, given the descending ordering (I removed non related fields for brevity)
{
"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": "..."
}