sql query looks like this and it works "SELECT Distance FROM UT_BEACON_INFO WHERE ReceivedDate >= \'" + currentDate.AddSeconds(-5) + "\' AND ReceivedDate <= \'" + currentDate.AddSeconds(5) + "\' AND SerialNumber = \'" + serialNumber + "\' AND ReceiverId = \'" + receiverId + "\'"
I’m trying to write a N1ql query that’s the same. I have tried two ways
SELECT BucketName.distance FROM B730FLS_DEV WHERE type = ‘UT_BEACON_INFO’ AND BucketName.receivedDate>= ‘" + currentDate.AddSeconds(-5) +
"’ AND BucketName.receivedDate <= ‘" + currentDate.AddSeconds(5) + "’ AND BucketName.serialNumber = ‘" + serialNumber + "’ AND BucketName.receiverId = ‘" + receiverId + "’"
SELECT BucketName.distance FROM B730FLS_DEV WHERE type = ‘UT_BEACON_INFO’ AND receivedDate>= ‘" + currentDate.AddSeconds(-5) +
"’ AND receivedDate <= ‘" + currentDate.AddSeconds(5) + "’ AND serialNumber = ‘" + serialNumber + "’ AND receiverId = ‘" + receiverId + "’".
@vsr1
Ok, I will try that. I had another question. If I have two document and wanted to all the information where the username equals each other in the two different types, what would the query be? "SELECT * FROM bucket WHERE type = ‘Type1’ AND Type1.userName= Type2.userName "? “SELECT bucket.type.Type1, bucket.type.Type2 FROM bucket WHERE type= type”?
I see what you mean, they are both 0 right now since I haven’t inserted anything right now. I’m in the process of converting a sql restful service to a couchbase one.
The following should work. If number of count on each type grows you need to measure performance impact.
INSERT INTO default VALUES("k001",{ "userName": "bob", "distance": 4, "type": "Type1" }), VALUES("k002",{ "userName": "bob", "height": 5, "type": "Type2" });
CREATE INDEX ix1 ON default(type);
SELECT d.*, t2.* FROM default d
LET t2 = FIRST v FOR v IN (SELECT RAW d2 FROM default d2 WHERE d2.type = "Type2") WHEN v.userName = d.userName END
WHERE d.type = "Type1" AND t2 IS NOT MISSING;
Thank you. I was wondering if you could help me with another query. If I wanted to join different documents in the same bucket where a variable in each document equals each other, how would I do this? For example
Couchbae JOINS are supported through field in document to Document key. i.e. parent, child relation needs to be present through document key
You can query each one independently joins through arrays as follows, but this may not perform well.
Best approach will be build prarent-child relation ship through document key.
SELECT d.*, t2.* FROM default d
LET t2 = FIRST v FOR v IN (SELECT RAW d2 FROM default d2 WHERE d2.type = "UT_ULD_INFO") WHEN v.serial = d.uldSerial END
WHERE d.type = "UT_BEACON_LOCATION" AND t2 IS NOT MISSING;
If “uldSerial” = “serial” has more than one match
SELECT t1.d.*,t2.* FROM (
SELECT d, t2 FROM default d
LET t2 = ARRAY v FOR v IN (SELECT RAW d2 FROM default d2 WHERE d2.type = "UT_ULD_INFO") WHEN v.serial = d.uldSerial END
WHERE d.type = "UT_BEACON_LOCATION" AND ARRAY_LENGTH(t2) > 0 ) AS t1 UNNEST t1.t2 AS t2;