Search code examples
databasecouchbasesql++

What is the syntax for inserting a document into Couchbase that contains an array as a value


I know the syntax to insert a document in to Couchbase using a n1ql INSERT statement if the values are single values:

INSERT INTO `travel-sample` ( KEY, VALUE )
  VALUES
  (
    "k001",
    { "id": "01", "type": "airline"}
  )
RETURNING META().id as docid, *;

But what if one of the values is an array of strings, what is the syntax for that? I haven't been able to find an example any where on the couchbase website or anywhere else.


Solution

  • There syntax for array is same as JSON syntax

    Example:

    reviews field is ARRAY of strings

    INSERT INTO `travel-sample` ( KEY, VALUE ) VALUES (
        "k001", { "id": "01", "type": "airline", "reviews": ["xyz", "abc"] });
    

    Document it self is ARRAY not a OBJECT

    INSERT INTO `travel-sample` ( KEY, VALUE ) VALUES (
        "k002", ["xyz", "abc"]);