Golang Ravendb giving wrong query result when using OrderBy - database

When making query with OrderBy it always query wrong result whereby repeating first document multiple times added to list.
q := session.Advanced().DocumentQueryAllOld(reflect.TypeOf(&models.List{}), "", "user", false)
//q = q.WhereNotEquals("Doc.hh", "Tarzan")
q = q.OrderBy("Docnn")
q = q.Statistics(&statsRef)
//q = q.Skip(0)
//q = q.Take(6)
q.ToList(&queryResult)
If no index before it will query right result but if existing index auto created by a different OrderBy value it will query wrong result

Related

Combine two JSON queries on Power BI

I am retrieving data from an Orchestrator (with a JSON code) to Power BI and for authentication I need to give the variable OrganizationUnitID as follows:
let
Path = "/odata/A",
Auth = Json.Document(Web.Contents(BaseUrl, [Headers=[#"Content-Type"="application/x-www-form-urlencoded"], Content=Text.ToBinary(Uri.BuildQueryString(Credentials))])),
Token = Auth[access_token],
A = Json.Document(Web.Contents(TenantUrl&Path, [Headers=[Accept="application/json", #"Authorization"="Bearer " & Token, #"OrganizationUnitId"="12345"]]))
in A
(PS: I did not post the entire query so that the post is not so long).
However, I would like to retrieve data for all OrganizationUnitIDs. These I can also retrieve as a query (= Id1 below), it currently comes as a list with 15 values:
let
Path = "/odata/Test",
Auth = Json.Document(Web.Contents(BaseUrl, [Headers=[#"Content-Type"="application/x-www-form-urlencoded"], Content=Text.ToBinary(Uri.BuildQueryString(Credentials))])),
Token = Auth[access_token],
Test = Json.Document(Web.Contents(TenantUrl&Path, [Headers=[Accept="application/json", #"Authorization"="Bearer " & Token]])),
value = Test[value],
#"Converted List to Table" = Table.FromList(value, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Records to Table" = Table.ExpandRecordColumn(#"Converted List to Table", "Column1", {"Name","Id"}, {"Name","Id"}),
Id1 = #"Expanded Records to Table"[Id]
in
Id1
My question is: can I / how can I combine this second query with the first one, so that in the first one I can include all the 15 OrganizationUnitIds?
I have tried some solutions posted online but so far none have worked.

How to use Django models in for loop?

Query = 'com pu'
Query = Query.split()
busi = ""
for data in Query:
busi += Business.objects.filter(Keywords__icontains=data)
When I use '+' symbol after busi variable for add all data according to filter that give error
Exception Type: TypeError
Exception Value:Can't convert 'QuerySet' object to str implicitly
How can I achieve that different Query part which split by that all give different-2 data and and both data in single busi object
from django.db.models import Q
Query = Query.split()
query = Q()
for data in Query:
query |= Q(keyword_name__icontains=data)
Business= Business.objects.filter(query)
Instead use &:
busi = None
q = Business.objects.filter(Keywords__icontains=data)
if busi is None:
busi = q
else:
busi = busi & q

Rails, passing an array as criteria to query

I'm calling the database based on the criteria passed from the view, so it has to be dynamic.
Let's say I have 2 arrays:
columns = ['col1', 'col2', 'col3']
vals = ['val1', 'val2', val3']
The query is easy to create, I can do concatenation, like
query = columns[0] + " = (?) AND" + ...
But what about the parameters?
#finalValues = MyTable.find(:all, :conditions => [query, vals[0], vals[1]... ])
But I don't know how many parameters I will receive. So while the query problem is solved with a for loop and concatenation, can I do something like:
#finalValues = MyTable.find(:all, :conditions => [query, vals])
And rails will understand that I'm not passing an array for an IN (?) clause, but to split the values for every individual (?)?
Or is my only option to do a full raw string and just go with it?
you can create condition array with query as first element and append all val element to it.
query = columns.map {|col| "#{col} = ?"}.join(" AND ")
#finalValues = MyTable.find(:all, :conditions => [query, *vals])
point of caution the columns and vals should have equal number of elements.

GAE-NDB: how prevent projection changed the results

I used ndb projection but it did change the results, how the results are not affected by projection?
class T(ndb.Model):
name = ndb.StringProperty()
name2 = ndb.StringProperty(repeated=True)
#classmethod
def test(cls):
for i in range(0, 10):
t = T(name=str(i))
if i%2 == 0:
t.name2=["zzz"]
t.put()
qr = T.query()
qo = ndb.QueryOptions(projection=['name', 'name2'])
items, cursor, more = qr.fetch_page(20, options=qo)
print len(items)
qo = ndb.QueryOptions(projection=['name'])
items, cursor, more = qr.fetch_page(20, options=qo)
print len(items)
The result is 5, 10
How to make result is 10, 10 ?
Thanks
An empty list-property (repeated=True) won't get indexed and as it's the index that projection queries use to return results, entities without values for the property won't be returned.
Your test case is susceptible to the eventual-consistency that Tim's comment mentions, but it isn't the only issue.

in gql (appengine), how do i check if an entity exists matching 2 filters before inserting a new one?

the 2 filtered fields would actually be a unique index in sql so i want to see if an entity exists based on these 2 fields before inserting a new one.
currently i have:
t2get = db.GqlQuery("SELECT __key__ FROM Talk2 WHERE ccc = :1 AND ms = :2", c, theDay)
for x in t2get:
theKey = x[0]
if theKey:
t2 = Talk2.get(theKey)
else:
t2 = Talk2()
which errors with:
UnboundLocalError: local variable 'theKey' referenced before assignment
if the entity doens't exist.
Any ideas?
If the two fields would actually be a unique index, maybe you should instead use them as the key_name. It will be faster and you can use a transaction, if needed.
def txn():
key_name = "%d.%d." % (c, theDay)
t2 = Talk2.get_by_key_name(key_name)
if not t2:
t2 = Talk2(key_name=key_name)
t2.put()
db.run_in_transaction(txn)
d'uh I figured it out. After hours trawling the web, 10 more minutes finds me the answer:
t2 = Talk2.all().filter('ccc =', c).filter('ms =', theDay).get()
returns the first entity (if any) ready to be edited.

Resources