Run Your First N1QL Query

    Now that you have a basic understanding of buckets and documents, you can try querying them using N1QL (pronounced "nickel"), the Couchbase Server query language.

About N1QL

    N1QL embraces the JSON document model and uses SQL-like syntax. In N1QL, you operate on JSON documents, and the result of your operation is another JSON document. You can run N1QL queries from the command line, using the cbq tool; or from the Query Workbench in the Couchbase Server Web Console.

A basic N1QL query has the following parts:

SELECT — The fields of each document to return.

FROM — The data bucket in which to look.

WHERE — The conditions that the document must satisfy.

    N1QL包含JSON文档模型,并使用类似sql的语法。在N1QL中,您对JSON文档进行操作,操作的结果是另一个JSON文档。您可以使用cbq工具从命令行运行N1QL查询;或从Couchbase服务器Web控制台中的查询工作台。

    一个基本的N1QL查询有以下几个部分:

    Select—要返回的每个文档的字段。

    WHERE-数据桶中查看。

    WHERE——文档必须满足的条件。

    Here’s an example of a basic N1QL query and the JSON document it returns. The query asks for the country that is associated with the airline Excel Airways:

SELECT country FROM `travel-sample` WHERE name="Excel Airways";

    Note that for all identifiers (bucket names) that contain a hyphen character, you need to enclose the name with backtick (`) characters.

The results:

{

    "requestID": "9e1cd084-f45e-4059-9e7a-edec30f60dd2",

    "signature": {

        "country": "json"

},

    "results": [

        {

            "country": "United Kingdom"

        }

    ],

    "status": "success",

"metrics": {

        "elapsedTime": "7.42097249s",

        "executionTime": "7.420925841s",

        "resultCount": 1,

        "resultSize": 51

    }

}

Try the Interactive Query Shell

    To run the interactive query shell, cbq, open a console window on your computer and enter the following:

bash -c "clear && docker exec -it db sh"

Then, navigate to the Couchbase bin directory, and start cbq:

cd /opt/couchbase/bin

./cbq -u Administrator -p password -engine=http://127.0.0.1:8091/

This displays the cbq shell prompt, against which you can enter N1QL commands, specifying your currently installed buckets. For example, the following query returns the different values that are used by the documents in the travel-sample bucket for the callsign field, limiting the number of results to five:

cbq>SELECT callsign FROM `travel-sample` LIMIT5;

The results:

{

    "requestID": "ae6fcd5b-e7f0-4725-ae1d-a38678c13a3e",

    "signature": {

        "callsign": "json"

    },

    "results": [

        {

            "callsign": "MILE-AIR"

        },

        {

            "callsign": "TXW"

        },

        {

            "callsign": "atifly"

        },

        {

            "callsign": null

        },

        {

            "callsign": "LOCAIR"

        }

    ],

    "status": "success",

    "metrics": {

        "elapsedTime": "33.748019ms",

        "executionTime": "33.673901ms",

        "resultCount": 5,

        "resultSize": 215

    }

}

    The results thus contain five callsign values. A callsign is associated with an airline; and airline is one of the document types that the travel-sample bucket contains. Others are airport and hotel.

    You can also search on a type. For example, the following query returns a maximum of one airport document, and lists all of the fields that it contains:

cbq>SELECT * FROM `travel-sample` WHERE type="airport" LIMIT 1;

    The results:

{

    "requestID": "c49a5885-9fde-40e3-871f-699f211078cc",

    "signature": {

        "*": "*"

    },

    "results": [

        {

            "travel-sample": {

                "airportname": "Calais Dunkerque",

                "city": "Calais",

                "country": "France",

                "faa": "CQF",

                "geo": {

                    "alt": 12,

                    "lat": 50.962097,

                    "lon": 1.954764

                },

                "icao": "LFAC",

                "id": 1254,

                "type": "airport",

                "tz": "Europe/Paris"

            }

        }

    ],

    "status": "success",

    "metrics": {

        "elapsedTime": "16.272029ms",

        "executionTime": "16.216091ms",

        "resultCount": 1,

        "resultSize": 489

    }

}

    The following query returns the names of (at a maximum) ten hotels that accept pets, in the city of Medway:

cbq> SELECT name FROM `travel-sample` WHERE type="hotel" AND city="Medway" and pets_ok=true LIMIT 10;

The results:

{

    "requestID": "b6dc75dd-4ed2-40de-83c8-9aebb3820ad8",

    "signature": {

        "name": "json"

    },

    "results": [

        {

            "name": "Medway Youth Hostel"

        }

    ],

    "status": "success",

    "metrics": {

        "elapsedTime": "45.380072ms",

        "executionTime": "45.326531ms",

        "resultCount": 1,

        "resultSize": 53

    }

}

The following query returns the name and phone fields for up to 10 documents for hotels in Manchester, where directions are not missing, and orders the results by name:

cbq> SELECT name,phone FROM `travel-sample` WHERE type="hotel" AND city="Manchester" and directions IS NOT MISSING ORDER BY name LIMIT 10;

The results:

{

    "requestID": "a3561cba-2377-4282-9c0f-68fc627950f6",

    "signature": {

        "name": "json",

        "phone": "json"

    },

    "results": [

    {

            "name": "Hilton Chambers",

            "phone": "+44 161 236-4414"

    },

        {

            "name": "Sachas Hotel",

            "phone": null

    },

        {

            "name": "The Mitre Hotel",

            "phone": "+44 161 834-4128"

        },

    ],

    "status": "success",

    "metrics": {

        "elapsedTime": "22.211069ms",

        "executionTime": "22.108582ms",

        "resultCount": 3,

        "resultSize": 253,

        "sortCount": 3

    }

}

Try the Query Workbench

The Couchbase Server Web Console includes the Query Workbench, an interactive tool that lets you compose and execute N1QL queries. To use the Query Workbench, log into the Couchbase Server Web Console, and then click Query:

Couchbase服务器Web控制台包含查询工作台,这是一个交互式工具,允许您组合和执行N1QL查询。要使用Query Workbench,登录到Couchbase Server Web控制台,然后单击Query:

The Query Workbench has three principal areas:

Query Editor: Where you will type your N1QL query

Bucket Insights: Provides information on the buckets that are currently maintained by your system. Right now, it shows that just one exists; the bucket travel-sample.

Query Results: Shows query results and provides a number of options for their display. To start with, you will use the default option, which is selectable by the JSON button, and duly displays results in JSON-format.

Use the Query Workbench to enter the following N1QL query:

SELECT name FROM `travel-sample` WHERE callsign = "MILE-AIR";

To execute your query, click Execute.

The results now appear in the Query Results panel:

As you can see, a single document was found to match your specified criterion — the document whose namevalue is 40-Mile Air (which is, in fact, the document you took an initial look at during the previous step in this Getting Started sequence).

Next

The final step in the Getting Started sequence, Choose Your Next Steps, offers suggestions for learning more about Couchbase and using it for production use-cases.

Other Destinations

N1QL from the SDK: Explains how to execute N1QL queries programmatically using the official Couchbase SDKs.

N1QL Query Language Tutorial: Provides interactive web modules where you can learn about N1QL without having Couchbase Server installed in your own environment. The modules are self-contained and let you modify and run sample queries. The tutorial covers SELECT statements in detail, including examples of JOIN, NEST, GROUP BY, and other typical clauses.

N1QL Cheat Sheet: Provides a concise summary of the basic syntax elements of N1QL. Print it out and keep it on your desk where it’ll be handy for quick reference.

N1QL Language Reference: Describes the N1QL language structure, including syntax and usage.

Couchbase Webinars: Live and recorded presentations by Couchbase engineers and product managers that highlight features and use-cases of Couchbase Server, including N1QL.

Couchbase Blog: Regularly-posted technical articles and announcements written by Couchbase employees, including SDK developers.

Couchbase Forum: A community resource where you can ask questions, find answers, and discuss N1QL with other developers and the Couchbase team.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,753评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,668评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,090评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,010评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,054评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,806评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,484评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,380评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,873评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,021评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,158评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,838评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,499评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,044评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,159评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,449评论 3 374
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,136评论 2 356

推荐阅读更多精彩内容