I would like to sort ArangoDB query results by various properties of a nested object however the bound vars seem not to work with dots in the names so
query: FOR a IN collection SORT #key ASC RETURN a
bindVars: #key = 'a.b.c.d'
(or) #key = 'a.x.y'
does not work
Is there a way how to "eval" the bound string to the nested property?
EDIT:
I found in the docs that
"key": [ "a", "b", "c" ] should work but it does not work for me.
The document reference (here: a) needs to remain in the query. It must not be part of the bind variable.
FOR a IN collection SORT a.#key ASC RETURN a
{ "key": ["b", "c", "d"] }
If you want to sort by two attributes:
FOR a IN collection SORT a.#key1 ASC, a.#key2 DESC RETURN a
{ "key1": ["b", "c", "d"], "key2": ["x", "y"] }
Related
I have following document structure:
{
"id": "1",
"aId": "2",
"bId": "3",
....
},
{ "id":"2",
"aId": "2",
"bId": "4"
}
How do i return for that JSON that has aId that has list of all bIds of the same aId, and as additional field: count of such bIds? So for example above and condtion: "WHERE aId="2" response would be:
{
"aId": "2",
"bIds" : ["4","3"],
"bIds count" : 2
}
Assuming i only pass one aId as parameter.
I tried something like:
select
(select 'something') as aId,
(select distinct value c.bId from c where c.aId='something') as bIds
from TableName c
But for love of me i cant figure out how to get that list + its count + hardcoded aId in single JSON response (single row)
For example this query:
select
(select distinct value 'someId') as aId,
(select distinct value c.bId) as bIds
from c where c.aId='someId'
will return
{ { 'aId': 'someId', 'bIds':'2'},{'aId':'someId','bIds':'4'}}
while what i acutally want is
{ {'aId':''someId', 'bIds':['2','4']}}
Here is query that is closest to what i want:
select
c.aId as aId,
count(c2) as bIdCount,
array(select distinct value c2.bId from c2)
from c join (select c.bId from c) as c2
where c.aId = 'SOME_ID'
Only thing line with array make this query fail if i delete this line it works (correctly returns id and count in one row). But i need to select content of this list also, and i ma lost why its not working, example is almost copypasted from "How to perform array projection Cosmos Db"
https://azurelessons.com/array-in-cosmos-db/#How_to_perform_array_projection_Azure_Cosmos_DB
Here is how you'd return an array of bId:
SELECT distinct value c.bId
FROM c
where c.aId = "2"
This yields:
[
"3",
"4"
]
Removing the value keyword:
SELECT distinct c.bId
FROM c
where c.aId = "2"
yields:
[
{ "bId" : "3" },
{ "bId" : "4" }
]
From either of these, you can count the number of array elements returned. If your payload must include count and aId, you'll need to add those to your JSON output.
I am working on a solr query that aims to retrieve documents that have other than specific values in array.
Example
{
id:1,
field:["a", "b", "c"]
},
{
id:2,
field:["a", "b"]
}
If i want to have documents which contains other than values "a" and "b", I expect to have this result from solr
{
id:1,
field:["a", "b", "c"]
}
If i want to have documents which contains other than values "a" and "b" and "c", I expect to have no result from solr
I tried to solve my problem with this query. I can get the right result for my first if but for the second, solr still sends me the document with id 1 even though it has no other value than "a", "b" and "c" in the field array.
# work
field: * AND (*:* NOT field:("a" OR "b"))
# doesn't work
field: * AND (*:* NOT field:("a" OR "b" OR "c"))
Does anyone have a solution to make this type of query with solr? :(
I need to split the string into substrings and pair them with the key value.
I'm new to the groovy language, so I'd appreciate it if you can help :)
I have this:
{"key": "a", "tag": ""},
{"key": "b", "tag": "one, two"}
I want to get this
{"key": "a", "tag": ""},
{"key": "b", "tag": "one"}
{"key": "b", "tag": "two"}
Use String::split(String) to split the strings.
Use Collection::flatten(Closure) to convert each entry object into any number of output objects (so for this particular problem: convert each entry with unsplit tag value into a separate entry for each comma-separated value in tag). You can also use Java 8 streams and the flatMap method to achieve the same result, flatten is just specific to Groovy (not necessarily better).
I don't think it would be good to give you a full solution, though, so I'll leave that up to you.
I need a query that can get me the document from a list of words for example if I use
select c from c join (SELECT distinct VALUE c.id FROM c JOIN word IN c.words WHERE word in('word1',word2) and tag in('motorcycle')) ORDER BY c._ts desc
it will bring both documents, I want to retrieve only the first one cause it matches the two words and not only one.
Document 1
"c": {
"id": "d0f1723c-0a55-454a-9cf8-3884f2d8d61a",
"words": [
"word1",
"word2",
"word3",
]}
Document 2
"c": {
"id": "d0f1723c-0a55-454a-9cf8-3884f2d8d61a",
"words": [
"word1",
"word4",
"word5",
]}
You should be able to cover this with two ARRAY_CONTAINS expressions in your WHERE clause (and no need for a JOIN):
SELECT c.id FROM c
WHERE ARRAY_CONTAINS(c.words, 'word1')
AND ARRAY_CONTAINS(c.words, 'word2')
This should return the id of your first document.
I have a database with documents in the following forms:
{"Type" : "A", "Date": "2013-09-19", "Week" : "A", "Day" : "Mon"}
{"Type" : "B", "Week" : "A", "Day" : "Mon", "Class" : "xyz"}
How do I create a view that will list all the classes (from doc.Type = "B") for a specific date (from doc.Type = "A")? Essentially it means matching the "Week" and "Day" fields. I have found examples (mostly based on Christopher Lenz's solution) but these are matching based on just one field being matched, which is unique in one of the document types.
Update
To clarify in response to #Daniel:
I would like be able to input a date into the URL query. This will match a type "A" doc. From this, I want all the type B docs that have the same Week and Day values as the original type A.
Reading this, I don't see how it can be done in Couch with just one query (you can only query one view at a time) since doc type B has no date field. No date in doc type B makes a single view with a compound key impossible. You could do it pretty easy in two queries with no doc changes/restructuring needed.
First query, by date on a type A view will give you the day and week values you need for second query.
Second query would run against a view with a compound key something like this...
"views": {
"type-A-by-date": {
"map": "function(doc) {
if (doc.type && (doc.type == 'A') {
emit([doc.date], [doc.week,doc.day]);
}
}"
},
"type-B-by-week-day": {
"map": "function(doc) {
if (doc.type && doc.type == 'B') {
emit([doc.week,doc.day], doc);
}
}"
},
Now you will have your type B docs that have week and day that match that original date query.
LMK if you need any more info :-)