I want a result as an array of objects in AQL.
Query:
db._query(aql`FOR doc IN ${collection} RETURN doc`).toArray();
I'd like to have this result...
{ "0": {"id": 1}, "1": {"id":2} }
...in the form of an array:
[{"id": 1}, {"id":2}]
The query you are currently using is returning all documents from the collection with all attributes, as an array.
If you only want to return specific attributes, you can refer to them inside the query's RETURN statement, e.g.
db._query(aql`FOR doc IN ${collection} RETURN doc.id`).toArray();
The above query will return an array of just the id values, so probably also not what you wanted.
If instead you want to return some attributes alongside their values, there are the following solutions:
Name the attributes explicitly in the RETURN part:
db._query(aql`FOR doc IN ${collection} RETURN { id: doc.id }`).toArray();
Use the AQL KEEP function:
db._query(aql`FOR doc IN ${collection} RETURN KEEP(doc, ["id"])`).toArray();
The latter approach will save typing if there are multiple attributes to return. In this case, you only have to add them to KEEP's second argument.
If the question is more about "how can I exclude certain attributes from being returned", then there is also the UNSET AQL function:
db._query(aql`FOR doc IN ${collection} RETURN UNSET(doc, ["_key", "_rev", "_id"])`).toArray();
This approach will return the document without the mentioned attributes.
Object.values({ "0": {"id": 1}, "1": {"id":2}})will return[{"id": 1}, {"id":2}]but never having used arango I suspect api has a cleaner approach