AQL how to return entire document instead of single item of document - arangodb

Below is the AQL Query.
for doc in `managed-function`
collect mkey = doc.mkey
return mkey
Above query returns only unique mkey list.
I want the entire doc to be returned, how can I achieve it ?

You need to add an INTO clause to the COLLECT operation, like this for example:
for doc in `managed-function`
collect mkey = doc.mkey into docs = doc
return {mkey, docs}
https://www.arangodb.com/docs/stable/aql/operations-collect.html#grouping-syntaxes

Related

slice on dict field containing list does'nt work

pymongo: 3.12.0
mongoengine: 0.23.1
I have a document:
class Logs(Document):
reference_id = StringField(default=None)
data = DictField(default=None)
In data field, i have a list failed_stories. This can have hundreds of elements and I want to perform pagination on it. So, i write this query as:
start_idx = 0
page_size = 10
reference_id = 'asdfg345678'
Logs.objects(reference_id=reference_id).fields(slice__data__failed_stories=[start_idx, page_size])
With this, i get one document in which all field are None except the dociment id (_id).
The following query results in document with correct data in document fields.
Logs.objects(reference_id=reference_id).get()
Is there any issue with the way I am writing this?
Note: I would like to do this with mongoengine only, if possible.

how to extract the properties from a neo4j cypher collection from the query result

I have a Neo4j Cypher query where I return the result as a COLLECTION like this:
RETURN COLLECT(r) AS Shipper
now I try to use
shippers = result.records[0].get('Shipper').properties;
but I get undefined value for shippers. I cannot seem to locate in the docs how to retrieve the properties from a COLLECTION. The raw collection data looks like this:
[{"identity":{"low":1112,"high":0},"start":{"low":320,"high":0},"end":{"low":447,"high":0},"type":"SHIPPED_WITH","properties":{"name":"DHL","account":"483097049790yrh"}},{"identity":{"low":1111,"high":0},"start":{"low":320,"high":0},"end":{"low":442,"high":0},"type":"SHIPPED_WITH","properties":{"name":"UPS","account":"12345T6741"}}]
I know I can avoid using the collection and retrieve the data in loop of some sort but I figure there is a better way. I am expecting to get an array of objects. Any help would be appreciated.
When you want to retrieve properties from a set of relationships, you could do this:
MATCH (n)-[r]->(m)
WHERE // your WHERE clause
// return a list of maps with keys `id` and `properties`
RETURN {id:id(r),properties:properties(r)}) AS Shipper
OR
// return just the properties as a map
RETURN properties(r) AS propertiesMap
OR
// return a collection of maps
RETURN COLLECT(properties(r)) AS listOfPropertyMaps

GUIDEWIRE : How to display the the query builder result in UI?

I am using query builder to return number of search result from the database table. Now I would like to display the result in the UI by only first three rows. How can I achieve this?
QueryAPI is lazy, when .toList(), .toTypedArray(), .toCollection(), .where(), etc occurs all resultset is retrieved (eager).
I recommend you to use this:
var limit = 3
var rs = Query.make(entity.XXX)...select()
rs.setPageSize(limit)
var paginatedRS = com.google.common.collect.Iterables.limit(rs,limit)
setPageSize method specifies how many rows will be fetch "by page"
limit method make a new iterator that have only the first (limit) rows

Can I filter multiple collections?

I want to filter multiple collections, to return only documents who have those requirements, the problem is when there is more than one matching value in one collection, the elements shown are repeated.
FOR TurmaA IN TurmaA
FOR TurmaB IN TurmaB
FILTER TurmaA.Disciplinas.Mat >10
FILTER TurmaB.Disciplinas.Mat >10
RETURN {TurmaA,TurmaB}
Screenshot of the problem
What your query does is to iterate over all documents of the first collection, and for each record it iterates over the second collection. The applied filters reduce the number of results, but this is not how you should go about it as it is highly inefficient.
Do you actually want to return the union of the matches from both collections?
(SELECT ... UNION SELECT ... in SQL).
What you get with your current approach are all possible combinations of the documents from both collections. I believe what you want is:
LET a = (FOR t IN TurmaA FILTER t.Disciplinas.Mat > 10 RETURN t)
LET b = (FOR t IN TurmaB FILTER t.Disciplinas.Mat > 10 RETURN t)
FOR doc IN UNION(a, b)
RETURN doc
Both collections are filtered individually in sub-queries, then the results are combined and returned.
Another solution would be to store all documents in one collection Turma and have another attribute e.g. Type with a value of "A" or "B". Then the query would be as simple as:
FOR t IN Turma
FILTER t.Disciplinas.Mat > 10
RETURN t
If you want to return TurmaA documents only, you would do:
FOR t IN Turma
FILTER t.Disciplinas.Mat > 10 AND t.Type == "A"
RETURN t
BTW. I recommend to call variables different from collection names, e.g. t instead of Turma if there is a collection Turma.

How to permanently unset an attribute of ArangoDB document?

I want to remove an attribute from a document in ArangoDB.
I thought the correct method for this was with the function UNSET(doc, attributeName1, ..., attributeNameN). However, with this alone, nothing is changed in the database.
Example:
let target_key = "42"
FOR doc IN myCollection
FILTER doc._key == target_key
RETURN UNSET(doc, "myAttribute")
The example returns the original document without the attribute myAttribute, but the new version is not saved to the database, so it seems this is only a projected copy.
Alternatively, you can set the attribute you want to remove to null and use the keepNull option:
LET target_key = "42"
FOR doc IN myCollection
FILTER doc._key == target_key
UPDATE doc WITH { myAttribute: null } IN myCollection
OPTIONS { keepNull: false }
RETURN NEW
Attributes with an explicit null value in the document are kept. Only the attributes specified after WITH are touched.
Note: if you explain the query, you will see nullMeansRemove: true under Write query options instead of keepNull: false.
The simple answer to this is to combine UNSET with the REPLACE function.
UNSET works on attributes in a single document, and REPLACE works on entire documents in a collection.
let target_key = "42"
FOR doc IN myCollection
FILTER doc._key == target_key
REPLACE UNSET(doc, "myAttribute") IN myCollection
RETURN NEW
This example returns the new document where myAttribute is removed with UNSET that is saved to the collection with REPLACE.
The NEW and OLD keywords can be used as with UPDATE and UPSERT.
I figured this out after I read this issue. I felt this was a too simple task to get stuck on, but I hope this is of use to people after me.

Resources