How to replace particular text from couchbase document?

n1ql

#1

I want search a particular text like (ftp:\) and want to replace to (http:\).
I want a bucket level query. It should search text from entire bucket documents and replace all wherever it found match.

My document structure is something like this,

BookPage.zip (2.1 KB)


#2

I tried this.

Any update operation requires the document itself and document’s ID as input.

cb.upsert(‘docid’, {‘property’: ‘value’})
cb.insert(‘docid’, {‘property’: ‘value’})
cb.replace(‘docid’, {‘property’: ‘value’})

But I dont know where this document added??
How to check whether it is added or not??
kissanime


#3

You need to aware of the path and field name you want to replace. If you want to update the filed text and starting string is ftp://

UPDATE default AS d 
SET d.text = REPLACE(d.text,"ftp://","http://") 
WHERE d.text LIKE "ftp://%";

UPDATE default AS d
SET d.thumbnail = REPLACE(d.thumbnail,"ftp://","http://"),
    d.pageAssets[pai] = REPLACE(pa,"ftp://","http://") FOR pai:pa IN d.pageAssets END,
    ps.url = REPLACE(ps.url,"ftp://","http://") FOR ps IN d.pageBgStyle END
WHERE d._type = "BookPage"
      AND (d.thumbnail LIKE "ftp://%"
           OR ANY pa IN d.pageAssets SATISFIES pa LIKE "ftp://%" END
           OR ANY ps IN d.pageBgStyle SATISFIES ps.url LIKE "ftp://%" END);