无法运行芒果查询

时间:2020-04-28 17:19:34

标签: couchdb mangodb couchdb-mango

这是我的查询:

{
   "selector": {
      "_id": {
         "$regex": "^rati"  //need to find all documents in ratings partition
      }
   },
   "fields": [
      "MovieID",
      "UserId",
      "Rating"
   ],
   "limit": 10,
   "sort": [
      {
         "MovieID": "asc"
      }
   ]
}

运行此查询时出现错误:Error running query. Reason: (no_usable_index) No global index exists for this sort, try indexing by the sort fields.

如果我删除

"sort": [
          {
             "MovieID": "asc"
          }
       ]

一切正常。老实说我快疯了,我不明白自己哪里错了。

我尝试过以下查询:

{
   "selector": {
      "_id": {
         "$regex": "^rati"
      },
      "MovieID": {
         "$gte": 0
      }
   },
   "fields": [
      "_id",
      "MovieID",
      "UserId",
      "Rating"
   ],
   "limit": 10,
   "sort": [
      {
         "_id": "asc"
      }
   ]
}

但相同。

1 个答案:

答案 0 :(得分:1)

您需要为字段MovieID创建一个索引

//Create via POST /db/_index HTTP/1.1 Content-Type: application/json

{
    "index": {
        "fields": ["MovieID"]
    },
    "name" : "MovieID-index",
    "type" : "json"
}

然后将字段MovieID包含在选择器中。

在此尝试:

{
   "selector": {
      "_id": {
         "$regex": "^rati"  //need to find all documents in ratings partition
      },
      "MovieID": {"$gte": 0} // include MovieID, If the ID is non-numeric change the selecor type.
   },
   "fields": [
      "MovieID",
      "UserId",
      "Rating"
   ],
   "limit": 10,
   "sort": [
      {
         "MovieID": "asc"
      }
   ]
}