Skip to content

Instantly share code, notes, and snippets.

@lbrenman
Created September 2, 2015 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbrenman/c8470fa233a26d0d276d to your computer and use it in GitHub Desktop.
Save lbrenman/c8470fa233a26d0d276d to your computer and use it in GitHub Desktop.
Appcelerator Arrow - How to query data that contains an array of elements

How to query data that contains an array of elements

See http://docs.appcelerator.com/arrowdb/latest/#!/guide/search_query and note the $all and $elemMatch operators

For this data set:

{
  "success": true,
  "request-id": "28551a17-8977-452a-9e34-2778d44e8b0a",
  "key": "myaccounts",
  "myaccounts": [
    {
      "id": "55e717634d27fa0bd89f1728",
      "name": "Account4",
      "type": "Prospect",
      "opportunities": [
        20
      ]
    },
    {
      "id": "55e717084d27fa0bd89f15b9",
      "name": "Account3",
      "type": "Client",
      "opportunities": [
        1,
        3,
        9
      ]
    },
    {
      "id": "55e71143d12d0b0beb9d8329",
      "name": "Account2",
      "type": "Client",
      "opportunities": [
        1,
        4,
        8
      ]
    },
    {
      "id": "55e7112d4d27fa0bd89ee72e",
      "name": "Account1",
      "type": "Prospect",
      "opportunities": [
        1,
        3,
        7
      ]
    }
  ]
}

The query where={"opportunities":{"$all":[ 3 ]}} returns the following:

{
  "success": true,
  "request-id": "e18e5ee1-8c5f-4705-9b46-4c88eb6aaf91",
  "key": "myaccounts",
  "myaccounts": [
    {
      "id": "55e717084d27fa0bd89f15b9",
      "name": "Account3",
      "type": "Client",
      "opportunities": [
        1,
        3,
        9
      ]
    },
    {
      "id": "55e7112d4d27fa0bd89ee72e",
      "name": "Account1",
      "type": "Prospect",
      "opportunities": [
        1,
        3,
        7
      ]
    }
  ]
}

The query where={"opportunities":{"$all":[ 1 ]}} returns the following:

{
  "success": true,
  "request-id": "05e420b4-eb42-4abc-a910-a928ab910d65",
  "key": "myaccounts",
  "myaccounts": [
    {
      "id": "55e717084d27fa0bd89f15b9",
      "name": "Account3",
      "type": "Client",
      "opportunities": [
        1,
        3,
        9
      ]
    },
    {
      "id": "55e71143d12d0b0beb9d8329",
      "name": "Account2",
      "type": "Client",
      "opportunities": [
        1,
        4,
        8
      ]
    },
    {
      "id": "55e7112d4d27fa0bd89ee72e",
      "name": "Account1",
      "type": "Prospect",
      "opportunities": [
        1,
        3,
        7
      ]
    }
  ]
}

The query where={"opportunities":{"$all":[ 1,3,9 ]}} returns the following:

{
  "success": true,
  "request-id": "d354553b-02c5-4d80-a443-956a2e940a25",
  "key": "myaccounts",
  "myaccounts": [
    {
      "id": "55e717084d27fa0bd89f15b9",
      "name": "Account3",
      "type": "Client",
      "opportunities": [
        1,
        3,
        9
      ]
    }
  ]
}

The query where={"opportunities":{ "$elemMatch": {"$gte": 15}}} returns the following:

{
  "success": true,
  "request-id": "dce7b2a0-fc9c-4879-b21b-5b582e461c3d",
  "key": "myaccounts",
  "myaccounts": [
    {
      "id": "55e717634d27fa0bd89f1728",
      "name": "Account4",
      "type": "Prospect",
      "opportunities": [
        20
      ]
    }
  ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment