Search code examples
javascriptnode.jssortingcloudant

how to use 'sort' when searching the cloudant database?


I am using Nano to connect to the database on NodeJS.

The api is: db.search(designname, searchname, [params], [callback])

In the params, I tried {q:"name:'39'"}, it can work, but if I add 'sort', then it never works. For example: {"name:'39'", sort:'count'}, {"name:'39'","['count']"}. However when I use the url to commit same operation like '/showall?q=name:39&sort="count"', and it works. I also tried {"name:'39'", limit:3}, it also work.

I just do not know how to use 'sort' in the JS code.


Solution

  • The sort parameter needs to be a JSON formatted string. In your url example, you are JSON formatting the string with /showall?q=name:39&sort="count". To replicate this in code you would need to do:

    db.search(ddocname, searchname, {"name":39', sort:'"count"'});