apache / couchdb-nano

Nano: The official Apache CouchDB library for Node.js
https://www.npmjs.com/package/nano
Apache License 2.0
651 stars 165 forks source link

Always got no matching index found, create an index to optimize query time #327

Closed Wildanzr closed 1 year ago

Wildanzr commented 1 year ago

I have a simple document like this:

{
  "_id": "1679325987388-adm-tuDahr9PKY",
  "_rev": "1-fd28138b2a26cbf379e051cec95eac71",
  "username": "usernameofdoctor",
  "password": "hashedpassword",
  "hospitalId": "idofhospital",
  "fullname": "fullnameofdoctor",
  "isActive": true,
  "createdAt": "2023-03-20T15:26:27.389Z",
  "updatedAt": null,
  "deletedAt": null,
  "refreshToken": null,
  "picture": "pictureofdoctor"
}

And the document design like this:

{
  "_id": "_design/3d624e08b57448199f8384e190ab57fc59651eae",
  "_rev": "1-1a00b9cb21f639016ea2d73b53970707",
  "language": "query",
  "views": {
    "doctors_index": {
      "map": {
        "fields": {
          "fullname": "asc",
          "username": "asc",
          "_id": "asc"
        },
        "partial_filter_selector": {}
      },
      "reduce": "_count",
      "options": {
        "def": {
          "fields": [
            "fullname",
            "username",
            "_id"
          ]
        }
      }
    }
  }
}

I want to implement rest api for this document with query included of page, limit, and search. The search query will be refer to fullname property of document. Then it will also sorting the result based of fullname in ascending. But I always got this error

no matching index found, create an index to optimize query time

Expected Behavior

I expected to got the result with sorted condition, but it always return error message.

Current Behavior

Possible Solution

Steps to Reproduce (for bugs)

  1. Create a db, add index to some property
  2. add some data in db
  3. Try find some document and sort using index property

Context

Your Environment

glynnbird commented 1 year ago

This is not a Nano issue but I'll advise you anyway :)

Your design document is creating an index on three things:

So a query involving all three items will hit the index, or hitting the first two and sorted by the second will work:

{
   "selector": {
      "fullname": "fullnameofdoctor",
      "username": "usernameofdoctor"
   },
   "sort": [
      "_id"
   ]
}

If you only want to query on fullname then change your index to only index fullname.