IBot
IBot

Reputation: 364

Delete all documents with null content from a Couchbase using N1QL

I executed a query with a little error on my couchbase server, this is executed query:

INSERT INTO `Writer_DB` (KEY _k, VALUE _v)
SELECT META().id _k, _v
from `Sam_DB` v

I used the v instead of _v so the operation was executed, but the result was that I have the Id's but I don't have the document's content, so when I search a document by its Id the content displayed is null.

I run again the query but it retrieved an error due the duplicate Id's so it would be nice to delete all documents with null content or "fill" the document content by Id. The thing is that I'm a newbie in couchbase and NoSql.

Upvotes: 1

Views: 1439

Answers (1)

vsr
vsr

Reputation: 7414

DELETE FROM `Writer_DB` AS d WHERE d IS NULL;

The above query requires primary index, to speed the query up the following index can be used:

create index ix1 on default(self) WHERE self IS NULL;
DELETE FROM `Writer_DB` AS d WHERE d IS NULL;

To update the documents

UPDATE `Writer_DB` SET id = META().id WHERE d IS NULL;

OR

DELETE FROM `Write_DB` USE KEYS (SELECT RAW META().id from `Sam_DB`);

Upvotes: 2

Related Questions