{"id":13596,"date":"2022-07-26T08:37:35","date_gmt":"2022-07-26T15:37:35","guid":{"rendered":"https:\/\/www.couchbase.com\/blog\/?p=13596"},"modified":"2025-06-13T17:20:45","modified_gmt":"2025-06-14T00:20:45","slug":"from-n1ql-to-javascript-and-back-part-2","status":"publish","type":"post","link":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/","title":{"rendered":"From N1QL to JavaScript and Back &#8211; Part 2: Basic Usage"},"content":{"rendered":"<p><a href=\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-1-introduction\/\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">The previous blog<\/span><\/a><span style=\"font-weight: 400;\"> introduced to you new features in JavaScript user-defined functions.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We are now going to explore them in more detail.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We start with a basic mechanism to execute N1QL statements inside JavaScript code.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">N1QL in JavaScript<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">There are two ways to execute N1QL statements inside a JavaScript function: either calling directly a function aptly named N1QL(), or by letting the JavaScript compiler detect embedded N1QL statements and produce corresponding N1QL() calls.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Each method is examined below.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">Example pre-requisites<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">The following sections contain a number of examples.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">These are fully functional but in order to execute them you will need:<\/span><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">a Couchbase cluster, version 7.1 or newer<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">a bucket named <\/span><i><span style=\"font-weight: 400;\">b1<\/span><\/i><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">a primary index on <\/span><i><span style=\"font-weight: 400;\">b1<\/span><\/i><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">to create each function in a library called <\/span><i><span style=\"font-weight: 400;\">udfblog<\/span><\/i><span style=\"font-weight: 400;\">, and<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">to create the corresponding N1QL UDF<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">You can use the UDF tab in the web console to create the function and the UDF.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">N1QL() function<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">The <em>N1QL()<\/em> function takes a statement in the form of a string, and a second optional parameter containing placeholder values.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">All it takes to execute a N1QL statement that does not take parameters and does not return values is the following:\u00a0<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-13598 size-large\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2022\/07\/snippet1-1024x101.jpg\" alt=\"\" width=\"900\" height=\"89\" srcset=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet1-1024x101.jpg 1024w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet1-300x29.jpg 300w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet1-768x75.jpg 768w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet1-1536x151.jpg 1536w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet1-20x2.jpg 20w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet1-1320x130.jpg 1320w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet1.jpg 2016w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">and the corresponding UDF:<\/span><\/p>\n<pre class=\"decode-attributes:false lang:default decode:true\">CREATE FUNCTION doInsert() LANGUAGE JAVASCRIPT AS \"doInsert\" AT \"udfblog\"<\/pre>\n<p><span style=\"font-weight: 400;\">(Forget for a second that the function itself does not return any value. Also, apologies for the code snippets shown as images, WordPress did not like rendering these as text).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">When the <em>N1QL()<\/em> function is called, the statement passed in the first parameter gets parsed, planned, and executed.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">If the statement returns no value, by the time the N1QL function returns, the statement has completed execution: in this particular case, assuming that bucket <\/span><i><span style=\"font-weight: 400;\">b1<\/span><\/i><span style=\"font-weight: 400;\"> exists and a key <\/span><i><span style=\"font-weight: 400;\">k1<\/span><\/i><span style=\"font-weight: 400;\"> does not already exist, a new document will be inserted.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">If you are familiar with ESQL\/C&#8217;s or PL\/SQL&#8217;s <\/span><em><span style=\"font-weight: 400;\">EXECUTE IMMEDIATE<\/span><\/em><span style=\"font-weight: 400;\"> statement, the <em>N1QL()<\/em> function is the JavaScript UDF equivalent.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">When values are returned, the return value of <em>N1QL()<\/em> is a JavaScript iterator, which can be used to collect values one at a time, as we will see later on.\u00a0<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">Dynamic N1QL<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Of course, the other use of the <em>N1QL()<\/em> function is to build a statement dynamically by concatenating strings, for example:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-13599\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2022\/07\/snippet2-1024x186.jpg\" alt=\"\" width=\"900\" height=\"163\" srcset=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet2-1024x186.jpg 1024w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet2-300x55.jpg 300w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet2-768x140.jpg 768w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet2-1536x279.jpg 1536w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet2-20x4.jpg 20w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet2-1320x240.jpg 1320w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet2.jpg 2014w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">While this is perfectly legal, it&#8217;s probably best not to employ such a technique given the risk of N1QL (or more generally SQL) injection that it carries.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Use placeholders instead.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">JavaScript Transpiler<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">The second way to execute N1QL is to instead embed the N1QL statement in the function body, and let the transpiler (a component of the JavaScript compiler) detect it and build the correct N1QL calls behind the scenes:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This is very convenient to execute static statements immediately, and the function suddenly becomes much more readable.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-13600\" src=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2022\/07\/snippet3-1024x102.jpg\" alt=\"\" width=\"900\" height=\"90\" srcset=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet3-1024x102.jpg 1024w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet3-300x30.jpg 300w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet3-768x76.jpg 768w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet3-1536x153.jpg 1536w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet3-20x2.jpg 20w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet3-1320x131.jpg 1320w, https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/07\/snippet3.jpg 2010w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/p>\n<h2><span style=\"font-weight: 400;\">N1QL statement placeholders and placeholder values<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">The best way to avoid the risks associated with dynamic N1QL generation is to have static statements using either named or positional placeholders, and passing placeholder values at execution time.<\/span><\/p>\n<h3><span style=\"font-weight: 400;\">N1QL() function<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">When using the <em>N1QL()<\/em> function, this can be done by passing the values in the second optional argument like the following.<\/span><\/p>\n<h4><span style=\"font-weight: 400;\">Positional parameters<\/span><\/h4>\n<p><span style=\"font-weight: 400;\">This case is covered by passing an array of values:<\/span><\/p>\n<pre class=\"decode-attributes:false lang:default decode:true\">function doInsert() {\r\n\u00a0\u00a0\u00a0\u00a0var q = N1QL(\"insert into b1 values(\\\"k4\\\", {\\\"f1\\\": $1})\", [4]);\r\n}<\/pre>\n<h4><span style=\"font-weight: 400;\">Named parameters<\/span><\/h4>\n<p><span style=\"font-weight: 400;\">In this case, just pass an object whose field name matches the parameter names:<\/span><\/p>\n<pre class=\"decode-attributes:false lang:default decode:true\">function doInsert() {\r\n\u00a0\u00a0\u00a0\u00a0var q = N1QL(\"insert into b1 values(\\\"k5\\\", {\\\"f1\\\": $p1})\", { p1: 5});\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">It is not possible to mix positional and named parameters, the <em>N1QL()<\/em> function only accepts one of either an array or an object.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Note that the values passed to <em>N1QL()<\/em> don&#8217;t have to be literals, you can construct arrays and objects from variables, or can pass an array or object variable:<\/span><\/p>\n<pre class=\"decode-attributes:false lang:default decode:true\">function doInsert() {\r\n\u00a0\u00a0\u00a0\u00a0let p1 = 6;\r\n\u00a0\u00a0\u00a0\u00a0var q = N1QL(\"insert into b1 values(\\\"k6\\\", {\\\"f1\\\": $1})\", [p1]);\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">or<\/span><\/p>\n<pre class=\"decode-attributes:false lang:default decode:true\">function doInsert() {\r\n\u00a0\u00a0\u00a0\u00a0let p1 = [7];\r\n\u00a0\u00a0\u00a0\u00a0var q = N1QL(\"insert into b1 values(\\\"k7\\\", {\\\"f1\\\": $1})\", p1);\r\n}<\/pre>\n<h3><span style=\"font-weight: 400;\">Transpiler<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">With the transpiler, only named parameters are possible &#8211; however, using them is as simple as using already declared variables:<\/span><\/p>\n<pre class=\"decode-attributes:false lang:default decode:true\">function doInsert() {\r\n\u00a0\u00a0\u00a0\u00a0let p1 = 8;\r\n\u00a0\u00a0\u00a0\u00a0var q = insert into b1 values(\"k8\", {\"f1\": $p1});\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">Of course, it is possible to use function parameters directly:<\/span><\/p>\n<pre class=\"decode-attributes:false lang:default decode:true\">function doInsert(p1) {\r\n\u00a0\u00a0\u00a0\u00a0var q = insert into b1 values(\"k9\", {\"k1\": $p1});\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">And the corresponding N1QL UDF:<\/span><\/p>\n<pre class=\"decode-attributes:false lang:default decode:true\">CREATE FUNCTION doInsert(p1) LANGUAGE JAVASCRIPT AS \"doInsert\" AT \"udfblog\"<\/pre>\n<h2><span style=\"font-weight: 400;\">Conclusion<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">We have covered basic examples on how to use N1QL inside javascript functions, both using the <em>N1QL()<\/em> function call and embedding the N1QL statement directly into JavaScript, and show how to use named and positional parameters.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The next blog post will cover iterator and data manipulation statements.<\/span><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The previous blog introduced to you new features in JavaScript user-defined functions. We are now going to explore them in more detail. We start with a basic mechanism to execute N1QL statements inside JavaScript code. N1QL in JavaScript There are [&hellip;]<\/p>\n","protected":false},"author":1782,"featured_media":12241,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[1816,9327,1812],"tags":[2133,1543,8911],"ppma_author":[8924],"class_list":["post-13596","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-couchbase-server","category-javascript","category-n1ql-query","tag-functions","tag-javascript","tag-udf"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.7.1 (Yoast SEO v25.7) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>N1QL in JavaScript &amp; N1QL() Function (Part 2: Basic Usage)<\/title>\n<meta name=\"description\" content=\"In the previous blog introduced to you new features in JavaScript user-defined functions. We are now going to explore them in more detail. Learn more.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"From N1QL to JavaScript and Back - Part 2: Basic Usage\" \/>\n<meta property=\"og:description\" content=\"In the previous blog introduced to you new features in JavaScript user-defined functions. We are now going to explore them in more detail. Learn more.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/\" \/>\n<meta property=\"og:site_name\" content=\"The Couchbase Blog\" \/>\n<meta property=\"article:published_time\" content=\"2022-07-26T15:37:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-14T00:20:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/10\/denormalization-guide-data-import-rdbms-tables-couchbase-n1ql-queries.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Marco Greco, Software Architect, Couchbase\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Marco Greco, Software Architect, Couchbase\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/\"},\"author\":{\"name\":\"Marco Greco, Software Architect, Couchbase\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/2b5184a7cdb443ff2897aff0866cd6fd\"},\"headline\":\"From N1QL to JavaScript and Back &#8211; Part 2: Basic Usage\",\"datePublished\":\"2022-07-26T15:37:35+00:00\",\"dateModified\":\"2025-06-14T00:20:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/\"},\"wordCount\":714,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/10\/denormalization-guide-data-import-rdbms-tables-couchbase-n1ql-queries.jpg\",\"keywords\":[\"functions\",\"javascript\",\"User Defined Function (UDF)\"],\"articleSection\":[\"Couchbase Server\",\"JavaScript\",\"SQL++ \/ N1QL Query\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/\",\"url\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/\",\"name\":\"N1QL in JavaScript & N1QL() Function (Part 2: Basic Usage)\",\"isPartOf\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/10\/denormalization-guide-data-import-rdbms-tables-couchbase-n1ql-queries.jpg\",\"datePublished\":\"2022-07-26T15:37:35+00:00\",\"dateModified\":\"2025-06-14T00:20:45+00:00\",\"description\":\"In the previous blog introduced to you new features in JavaScript user-defined functions. We are now going to explore them in more detail. Learn more.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/#primaryimage\",\"url\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/10\/denormalization-guide-data-import-rdbms-tables-couchbase-n1ql-queries.jpg\",\"contentUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/10\/denormalization-guide-data-import-rdbms-tables-couchbase-n1ql-queries.jpg\",\"width\":1200,\"height\":628,\"caption\":\"Learn how to import and denormalize your data from a relational database into Couchbase using N1QL\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.couchbase.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"From N1QL to JavaScript and Back &#8211; Part 2: Basic Usage\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#website\",\"url\":\"https:\/\/www.couchbase.com\/blog\/\",\"name\":\"The Couchbase Blog\",\"description\":\"Couchbase, the NoSQL Database\",\"publisher\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.couchbase.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\",\"name\":\"The Couchbase Blog\",\"url\":\"https:\/\/www.couchbase.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png\",\"contentUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png\",\"width\":218,\"height\":34,\"caption\":\"The Couchbase Blog\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/2b5184a7cdb443ff2897aff0866cd6fd\",\"name\":\"Marco Greco, Software Architect, Couchbase\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/image\/707c967b795fd71b6330f6d3118cf308\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e6be0ee56851d2f71a554731d5edd5c820069680f0a810b47f094091c58bc553?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e6be0ee56851d2f71a554731d5edd5c820069680f0a810b47f094091c58bc553?s=96&d=mm&r=g\",\"caption\":\"Marco Greco, Software Architect, Couchbase\"},\"description\":\"In a previous life, Marco used to be CTO, radiation physicist, software architect, sysadmin, DBA, trainer and general handyman at Italy's largest radiation theraphy practice. Having switched career and country, he spent more than two decades in various support and development positions in Informix first and IBM later, before finally taking the plunge and joining Couchbase, to help them make gold out of N1QL. He holds several patents and has authored open source projects of his own.\",\"sameAs\":[\"https:\/\/github.com\/marcogrecopriolo\",\"https:\/\/www.linkedin.com\/in\/marco-greco-7665308\/\"],\"url\":\"https:\/\/www.couchbase.com\/blog\/author\/marcocouchbase-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"N1QL in JavaScript & N1QL() Function (Part 2: Basic Usage)","description":"In the previous blog introduced to you new features in JavaScript user-defined functions. We are now going to explore them in more detail. Learn more.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/","og_locale":"en_US","og_type":"article","og_title":"From N1QL to JavaScript and Back - Part 2: Basic Usage","og_description":"In the previous blog introduced to you new features in JavaScript user-defined functions. We are now going to explore them in more detail. Learn more.","og_url":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/","og_site_name":"The Couchbase Blog","article_published_time":"2022-07-26T15:37:35+00:00","article_modified_time":"2025-06-14T00:20:45+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/10\/denormalization-guide-data-import-rdbms-tables-couchbase-n1ql-queries.jpg","type":"image\/jpeg"}],"author":"Marco Greco, Software Architect, Couchbase","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Marco Greco, Software Architect, Couchbase","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/#article","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/"},"author":{"name":"Marco Greco, Software Architect, Couchbase","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/2b5184a7cdb443ff2897aff0866cd6fd"},"headline":"From N1QL to JavaScript and Back &#8211; Part 2: Basic Usage","datePublished":"2022-07-26T15:37:35+00:00","dateModified":"2025-06-14T00:20:45+00:00","mainEntityOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/"},"wordCount":714,"commentCount":0,"publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/10\/denormalization-guide-data-import-rdbms-tables-couchbase-n1ql-queries.jpg","keywords":["functions","javascript","User Defined Function (UDF)"],"articleSection":["Couchbase Server","JavaScript","SQL++ \/ N1QL Query"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/","url":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/","name":"N1QL in JavaScript & N1QL() Function (Part 2: Basic Usage)","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/#primaryimage"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/10\/denormalization-guide-data-import-rdbms-tables-couchbase-n1ql-queries.jpg","datePublished":"2022-07-26T15:37:35+00:00","dateModified":"2025-06-14T00:20:45+00:00","description":"In the previous blog introduced to you new features in JavaScript user-defined functions. We are now going to explore them in more detail. Learn more.","breadcrumb":{"@id":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/#primaryimage","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/10\/denormalization-guide-data-import-rdbms-tables-couchbase-n1ql-queries.jpg","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2021\/10\/denormalization-guide-data-import-rdbms-tables-couchbase-n1ql-queries.jpg","width":1200,"height":628,"caption":"Learn how to import and denormalize your data from a relational database into Couchbase using N1QL"},{"@type":"BreadcrumbList","@id":"https:\/\/www.couchbase.com\/blog\/from-n1ql-to-javascript-and-back-part-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.couchbase.com\/blog\/"},{"@type":"ListItem","position":2,"name":"From N1QL to JavaScript and Back &#8211; Part 2: Basic Usage"}]},{"@type":"WebSite","@id":"https:\/\/www.couchbase.com\/blog\/#website","url":"https:\/\/www.couchbase.com\/blog\/","name":"The Couchbase Blog","description":"Couchbase, the NoSQL Database","publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.couchbase.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.couchbase.com\/blog\/#organization","name":"The Couchbase Blog","url":"https:\/\/www.couchbase.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png","width":218,"height":34,"caption":"The Couchbase Blog"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/2b5184a7cdb443ff2897aff0866cd6fd","name":"Marco Greco, Software Architect, Couchbase","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/image\/707c967b795fd71b6330f6d3118cf308","url":"https:\/\/secure.gravatar.com\/avatar\/e6be0ee56851d2f71a554731d5edd5c820069680f0a810b47f094091c58bc553?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e6be0ee56851d2f71a554731d5edd5c820069680f0a810b47f094091c58bc553?s=96&d=mm&r=g","caption":"Marco Greco, Software Architect, Couchbase"},"description":"In a previous life, Marco used to be CTO, radiation physicist, software architect, sysadmin, DBA, trainer and general handyman at Italy's largest radiation theraphy practice. Having switched career and country, he spent more than two decades in various support and development positions in Informix first and IBM later, before finally taking the plunge and joining Couchbase, to help them make gold out of N1QL. He holds several patents and has authored open source projects of his own.","sameAs":["https:\/\/github.com\/marcogrecopriolo","https:\/\/www.linkedin.com\/in\/marco-greco-7665308\/"],"url":"https:\/\/www.couchbase.com\/blog\/author\/marcocouchbase-com\/"}]}},"authors":[{"term_id":8924,"user_id":1782,"is_guest":0,"slug":"marcocouchbase-com","display_name":"Marco Greco, Software Architect, Couchbase","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/e6be0ee56851d2f71a554731d5edd5c820069680f0a810b47f094091c58bc553?s=96&d=mm&r=g","author_category":"","last_name":"Greco","first_name":"Marco","job_title":"","user_url":"","description":"In a previous life, Marco used to be CTO, radiation physicist, software architect, sysadmin, DBA, trainer and general handyman at Italy's largest radiation theraphy practice.\r\n\r\nHaving switched career and country, he spent more than two decades in various support and development positions in Informix first and IBM later, before finally taking the plunge and joining Couchbase, to help them make gold out of N1QL.\r\n\r\nHe holds several patents and has authored open source projects of his own."}],"_links":{"self":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts\/13596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/users\/1782"}],"replies":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/comments?post=13596"}],"version-history":[{"count":0,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts\/13596\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/media\/12241"}],"wp:attachment":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/media?parent=13596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/categories?post=13596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/tags?post=13596"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=13596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}