GrimThor3
GrimThor3

Reputation: 171

Adding Empty Document to Couchbase bucket

As context, I am creating a bucket of key values with empty documents to fulfill a want to quickly check IDs just through checking key existence in comparison to checking values. In the cluster, I have two buckets, source-bucket and new-bucket. The documents in source-bucket are in the form:

ID: {
ID: ...,
type: ...
}

You can move the contents of source to the new bucket using the query

INSERT INTO `new-bucket` (KEY k, VALUE v) SELECT meta(v).id AS k FROM `source-bucket` as v

Is there a way to copy over just the key? Something along the lines of this (although this example doesn't work):

INSERT INTO `new-bucket` (KEY k, VALUE v) values (SELECT meta().id FROM `source-bucket`, NULL)

I guess I'm not familiar enough with the n1ql syntax to under how to construct a query like this. Let me know if you have an answer to this. If this is a duplicate, feel free to point to the answer.

Upvotes: 2

Views: 261

Answers (1)

vsr
vsr

Reputation: 7414

If you need empty object use {}.

CREATE PRIMARY INDEX ON `source-bucket`;

INSERT INTO `new-bucket` (KEY k, VALUE {}) 
SELECT meta(b).id AS k FROM `source-bucket` as b

NOTE: document value can be empty object or any data type. The following all are valid.

  INSERT INTO default VALUES ("k01", {"a":1});
  INSERT INTO default VALUES ("k02", {});
  INSERT INTO default VALUES ("k03", 1);
  INSERT INTO default VALUES ("k04", "aa");
  INSERT INTO default VALUES ("k05", true);
  INSERT INTO default VALUES ("k06", ["aa"]);
  INSERT INTO default VALUES ("k07", NULL);

Upvotes: 2

Related Questions