3

If I have documents like so, is there a mango query that I can use to find all documents where the uid in the signatures array matches the uid in the root document?

{
    _id: "1",
    uid: "12345",
    signatures: [ { uid: "12345" } ]
},
{
    _id: "2",
    uid: "12345",
    signatures: [ { uid: "55555" } ]
}

Expected response would only give me the first document.

bryan
  • 8,879
  • 18
  • 83
  • 166

1 Answers1

2

You can't refer to values, only keys - so you can check if a particular "uid" is in the array with

{
   "selector": {
      "signatures": {
         "$elemMatch": {
            "$eq": {
               "uid": "12345"
            }
         }
      }
   },
   "fields": [
      "_id",
      "_rev"
   ]
}

but you can't state that the uid should be equal to the uid field outside the array.

It's way easier to achieve what you want with a view, of course:

function(doc) {
  doc.signatures.forEach(function(elem) {
    if (elem.uid == doc.uid) {
      emit(null, null);
      return
    }
  });
}
xpqz
  • 3,617
  • 10
  • 16