N1QL has variables and loops for ARRAYs you can check documentation. Query results are considered as ARRAY and you can loop through or you can use SDKS.
I have checked that link, that has information about working with arrays. I have provided a sample of the scenario in tsql that I am trying to achieve with n1ql.
declare @table as table (
ID int,
name varchar(15)
)
insert into @table values (1,‘Dany’)
insert into @table values (2,‘John’)
insert into @table values (3,‘Peter’)
insert into @table values (4,‘falcon’)
declare @ID int
declare @curs as cursor
set @curs = cursor fast_forward for
select ID from @table
open @curs
fetch next from @curs into @ID
while(@@FETCH_STATUS=0)
begin
Declare @name varchar(15) = (select name from @table where ID = @ID)
print 'Printing name of ID : ’ + convert(varchar(2),@ID)
print @name
fetch next from @curs into @ID
end
close @curs
deallocate @curs
I am trying to collect the data metadata for the all the documents and frame them tsql to load the data into mssql…I am able to acheive this but, it is taking too long for 40K rows to process.
looking for any better option that completes with in seconds…
import sys
import os
import subprocess
import requests
import json
import pyodbc
from requests.auth import HTTPBasicAuth
from couchbase.cluster import Cluster
from couchbase.cluster import PasswordAuthenticator
Does this work?
SELECT ARRAY_DISTINCT(ARRAY v[0] FOR v IN PAIRS(d) END)FROM bucketname AS d WHERE META().id in (SELECT distinct META().id FROM bucketname)
Basically you want get field names. JSON can have nested objects and arrays, array of objects etc… There is no easy way to get the paths for all possible combinations. The PAIRS() only gives current level field name not path (nested object, array).
You don’t need subquery. Equivalent query is
SELECT ARRAY_DISTINCT(ARRAY v[0] FOR v IN PAIRS(d) END)FROM bucketname AS d WHERE META().id IS NOT NULL;
If you need field names on top level
SELECT OBJECT_NAMES(d) FROM bucketname AS d WHERE META().id IS NOT NULL;