4

I'm not sure how to figure out how to sort my documents using a Mango query. Any help would be greatly appreciated:


I do the following find query and get the following error:

return db.find({ sort: ['timestamp'], selector: {$and: [{type:"revision"},{doc_id: doc_id}] } })

Note, if I do the following I also get the same error:

return db.find({ sort: ['timestamp'], selector: {$and: [{type:"revision"}] } })

{ error: 'no_usable_index', reason: 'No index exists for this sort, try indexing by the sort fields.', name: 'no_usable_index', status: 400, message: 'No index exists for this sort, try indexing by the sort fields.' }

I created a filter like so:

db.createIndex({
  index: {fields: ['type', 'timestamp']}
})

This is what pops up in my design documents:

{
  "_id": "_design/b661014513bf0c13270a9033d6d1c1853a8b5dbf",
  "_rev": "1-1d871456b0a78952845705f4e8dd2c26",
  "language": "query",
  "views": {
    "b661014513bf0c13270a9033d6d1c1853a8b5dbf": {
      "map": {
        "fields": {
          "type": "asc",
          "timestamp": "asc"
        },
        "partial_filter_selector": {}
      },
      "reduce": "_count",
      "options": {
        "def": {
          "fields": [
            "type",
            "timestamp"
          ]
        }
      }
    }
  }
}
bryan
  • 7,656
  • 12
  • 65
  • 136
  • Please add some more information, what result do you want from the `query`? – Rohit Khatri Jun 29 '18 at 14:00
  • 1
    @RohitKhatri What do you mean, looks like you skimmed the question too fast. I want the query to work instead of getting an error message that I provided in yellow above. – bryan Jun 29 '18 at 14:01
  • This might help https://stackoverflow.com/questions/46689286/cloudant-no-index-exists-for-this-sort-try-indexing-by-the-sort-fields – Rohit Khatri Jun 29 '18 at 14:11

0 Answers0