0

Okay, so my problem is that I have a multikey view, that works.

function (docu) {
  if(!docu.deleted){
    emit([docu.key1, docu.key2, docu.key3], docu);
  }
}

I am attempting to add another key to this already multi-key array. The key I am attempting to add is an array of string values,

[
"test",
"test2"
]

I am querying my view using PouchDB and its built in query method, code is below.

database.query("views/viewTest", {
    startkey: [key1, key2, key3, key4],
    endkey: [key1, key2, key3, key4],
    reduce: false
}, function (err, res) {
    if (err) {
        console.log(err);
        alert(err);
    } else {
        console.log(res);
    }
});

Where key4 is my new array. I have a little luck with my map function by changing it to emit([docu.key1, docu.key2, docu.key3, [docu.key4]], docu); but this only works if there is only one item in the array, and I need it to work for multiple items in the array.

I can't figure out how to set up my view or query to utilize multiple items in my key4 array.

I have tried querying with startkey and endkey, where key4 had an {} pushed into it for the endkey, but this returned everything. I only want the values returned within the key4 array, as long as they match the other keys as well.

So to sum:

  • My view/query works before I add in the new key4 array.
  • The view/query works if the key4 array has ONE item in it.
  • I need it to work for ALL items in the key4 array.
  • I have tried multiple things and can/could not get this to work.

Any suggestions?

Hypnic Jerk
  • 1,192
  • 3
  • 14
  • 32

1 Answers1

0

Why don't you use a unique array that concats key4 to [key1, key2, key3]?

function (docu) {
  if(!docu.deleted){
    var key = [docu.key1, docu.key2, docu.key3].concat(doc.key4);
    emit(key, docu);
  }
}
DaniCE
  • 2,412
  • 1
  • 19
  • 27
  • They problem is that `key4` from the Document will always be one item, a `String`, whereas when I search in the `start/endkey` it will be an `Array` of `String`s. So I need a way for the `map` to be able to grab the input and test if `key4` from the document is inside the `start/endkey` key4, and emit those. – Hypnic Jerk Feb 17 '17 at 15:21