I have a field in a table called duration that contains a JSON string like this:
{
"videos": {
"en":"00:03:11",
"es":"00:03:11"
},
"audios": {
"en":"00:00:03",
"es":"00:00:03"
}
}
Is it possible to execute a query to sum all the values of both videos and audio keys using the possible langauges? Meaning that given this JSON structure, I'd like a query to return:
00:06:28
EDIT:
Don't mind the format of the values for the moment, there are ways to sum datetime values in SQL. What I'm struggling with now is to traverse the values in the JSON to actually sum them.
I'm experiencing an issue with apollo client caching, and I'm not sure I understand why. I'm building an angular 6 app, using apollo-boost, all is working well. I have a scenario now where my a graphql query takes an id (of a user) and a filter string (used to filter records on the backend). The angular component code looks like:
ngOnInit() {
this.filter$.subscribe(filterValue => {
this.route.params.subscribe(this.getAppointments.bind(this, filterValue));
});
}
The getAppointments function looks like:
getAppointments(filter: string, params: {id: string}) {
this.artistAppointmentBookGQL.watch({artistId: user.artist._id, filter}).valueChanges
.pipe(
map(results => {
// THIS ALWAYS RUNS WHEN THE FILTER CHANGES
// HOWEVER THE RESULTS ARE ALWAYS THE LAST QUERY RUN
// IF THE FILTER HAS BEEN RUN BEFORE
console.log(user.artist._id, filter, results.data.artist.appointmentBook);
return results.data.artist.appointmentBook;
}));
}
The graphql query:
query artistAppointmentBook($artistId: ID!, $filter: String) {
artist(id: $artistId, appointmentType: $filter) {
_id
appointmentBook {
_id
created_at
firstName
lastName
date
price
stripe {
charge {
id
amount
}
}
}
}
The main issue:
I have 4 different possible filter values (all, unconfirmed, confirmed, paid). When I run these queries with each filter value, it works as expected, and I get back the proper result sets from the apollo server. However, as soon as I run the same query twice, I only get back the result of whatever the last query was, and no network call is made, presumably because it's using a cached version.
Shouldn't the cache be based on the variable inputs? It seems to run fine the first time I run with different variables, but as soon as one gets duplicated I only get back whatever the last call yielded. Thanks for any help!
This gif demonstrates the issue:
Figured out my issue. As it should, apollo is caching the artist record because it has an _id and a type. The filter was being passed into artist query, when it should have been passed in at the appointmentBook level. I updated my schema to accept the filter param and then passed it in there instead of into the artist query.
Originally I had:
query artistAppointmentBook($artistId: ID!, $filter: String) {
artist(id: $artistId, appointmentType: $filter) {
_id
appointmentBook {
_id
created_at
firstName
lastName
date
price
stripe {
charge {
id
amount
}
}
}
}
Which needed to be changed to:
query artistAppointmentBook($artistId: ID!, $filter: String) {
artist(id: $artistId) {
_id
appointmentBook(filter: $filter) {
_id
created_at
firstName
lastName
date
price
stripe {
charge {
id
amount
}
}
}
}
After this update, the queries are cached properly.
I have some Couchbase data in the following format
{
"id": "12343",
"transaction": {
"2018-01-11": 10,
"2017-12-01" : 20
},
"_type": "TransactionData"
}
I would like to get the ids whose transaction list contains key older than a given date ( for example, this object would not be retrieved for a value of "2017-11-01", but it does for "2017-12-12".
I made a view, but I would like to parameterise the date String:
function (doc, meta) {
if (doc._type == 'TransactionData') {
for (var key in doc.transaction) {
//I want to send the String value from java
if (key < "2018-02-21") {
emit(doc.id, null);
break;
}
}
}
}
I tried writing some N1QL query, but my server doesn't allow that, I can't change this configuration.
I don't think I can use the startKey, because I return a map of (id, null) pairs.
How can I filter the ids that have transactions older than a configurable date?
Thanks.
You can do like this:
function (doc, meta) {
if (doc._type == 'TransactionData') {
for (var key in doc.transaction) {
emit(doc.id, null);
}
}
}
user _count for Reduce function, then you can query using
query.range("2018-02-21", {}).reduce(true)
then you can take the value to see how many rows there are
Views are static indexes. Documents are processed once after each change, and any emitted results put into the index. You can't parameterize your function because it isn't rerun for every query. So you can't solve the problem the way you're approaching it. (You can do that with N1QL.)
Typically you solve this by adding a key range filter to your query. Look at the documentation for querying views. There are examples on how to select by date. You'll have to decide how you want to structure the index (view) you create.
Given a JSON document on couchbase, for example, a milestone collections, which is similar to this:
{
"milestoneDate" : /Date(1335191824495+0100)/,
"companyId" : 43,
"ownerUserId": 475,
"participants" : [
{
"userId": 2,
"docId" : "132546"
},
{
"userId": 67,
"docId" : "153"
}
]
}
If I were to select all the milestones of the company 43 and want to order them by latest first.. my view on couchbase would be something similar to this:
function (doc, meta) {
if(doc.companyId && doc.milestoneDate)
{
//key made up of date particles + company id
var eventKey = dateToArray(new Date(parseInt(doc.milestoneDate.substr(6))));
eventKey.push(doc.companyId);
emit(eventKey, null);
}
}
I do get both dates and the company Id on rest urls.. however, being quite new to couchbase, I am unable to work out how to restrict the view to return only milestones of company 43
The return key is similar to this:
"key":[2013,6,19,16,11,25,14]
where the last element (14) is the company id.. which is quite obviously wrong.
The query parameters that I have tried are:
&descending=true&startkey=[{},43]
&descending=true&startkey=[{},43]&endKey=[{},43]
tried adding companyId to value but couldn't restrict return results by value.
And according to couchbase documentation I need the date parts in the beginning to sort them. How do I restrict them by company id now, please?
thanks.
Put the company id at the start of the array, and because you'll be limiting by company id, couchbase sorts by company id and then by date array so you will be only ever getting the one company's milestone documents
I'd modify the view to emit
emit([doc.copmanyId, eventKey], null);
and then you can query the view with
&descending=true&startkey=[43,{}]
This was what worked for me previously..
I went back and tried it with end key and this seems to work - restricts and orders as required:
&descending=true&startkey=[43,{}]&endkey=[42,{}]
or
&descending=true&startkey=[43,{}]&endkey=[43,{}]&inclusive_end=true
either specify the next incremented/decremented value (based on descending flag) with end key, or use the same endkey as startkey and set inclusiveEnd to true
Both of these options should work fine. (I only tested the one with endkey=42 but they should both work)
I presume a simple question. I have the following data.
I want to search for all rows where the ID is > 2 but < 8 and the Price is > 30
I have used various versions of: startkey=["2", null] or even something like startkey=["2", "30"] just for testing.
It only ever seems to run both conditions on the first row. So if I do: startkey=["2", "30"] then I get back:
{"id":"3","key":["3","30"],"value":null},
{"id":"4","key":["4","30"],"value":null},
{"id":"5","key":["5","20"],"value":null},
{"id":"6","key":["6","60"],"value":null},
{"id":"8","key":["8","60"],"value":null}
Why is row 5 there?
I am starting to get the view that I need to handle this in the code (.net) and make multiple calls somehow... I can't seem to find anything on this that works....
Note: I have tried doing say a loop with for (i = 0; i < doc.ID.length; i++) and then using doc.ID[i] but it never returns anything....
Currently I just have
function (doc, meta) {
emit([doc.ID, doc.Price ],null);
}
Essentially I want to have a search where there are 5 input keys that a user has. So do I need to make 5 calls and then keep taking data from the previous output as the source for the next???
Other references I have looked at include: the manual
Thanks in advance,
Kindest Regards
Robin
This is a common misconception, with a compound array index key, it's still treated as a string, therefore the index key [2,10] is actually "[2,10]", and the index key [5,20], is actually "[5,20]".
So the reason that startkey=["2", "30"]shows the {"id":"5","key":["5","20"],"value":null}, row is because as a string it is > startkey.
Likewise, the Query startkey=[2,10]&endkey=[5,10] returns
{"total_rows":7,"rows":[
{"id":"2","key":[2,20],"value":null},
{"id":"3","key":[3,30],"value":null},
{"id":"4","key":[4,30],"value":null}
]
}
because startkey="[2,10]" < "[2,20]" && "[4,30]" < "[5,10]"=endkey, but "[5,20]" is not within that string Range.
Range Queries with startkey and endkey
startkey => endkey is a Range query using strcmp(), the group and group level is based on the string, where the comma is separating string tokens.
A Good Reference Link (since Couchbase Views work much like Apache CouchDB Views (inspired by them))
http://wiki.apache.org/couchdb/View_collation#Collation_Specification
Spatial View/Query
To achieve the result you are trying for, you could also write a Spatial View to have multi-dimensional Queries, numeric only. While you might not initially think of it
function (doc, meta) {
emit({
type: "Point",
coordinates: [doc.ID, doc.Price]
}, meta.id);
}
The Query would be a Bounding Box Query:
&bbox=2,0,8,30
{"total_rows":0,"rows":[
{"id":"2","bbox":[2,20,2,20],"geometry":{"type":"Point","coordinates":[2,20]},"value":"2"},
{"id":"3","bbox":[3,30,3,30],"geometry":{"type":"Point","coordinates":[3,30]},"value":"3"},
{"id":"4","bbox":[4,30,4,30],"geometry":{"type":"Point","coordinates":[4,30]},"value":"4"},
{"id":"5","bbox":[5,20,5,20],"geometry":{"type":"Point","coordinates":[5,20]},"value":"5"}
]
}
Another Query:
&bbox=2,30,8,30
{"total_rows":0,"rows":[
{"id":"3","bbox":[3,30,3,30],"geometry":{"type":"Point","coordinates":[3,30]},"value":"3"},
{"id":"4","bbox":[4,30,4,30],"geometry":{"type":"Point","coordinates":[4,30]},"value":"4"}
]
}