Want to get entire document Using N1ql couchbase - n1ql

I want to get entire document in a bucket using N1ql
My document Name is CR_5031_114156723_2016-08-02 where other then CR numbers will be different for different document.
I tried with below 2 query but getting only OrderDetails details
select d.* from Delivery.OrderDetails d where d.orderId in ['114156723']
select d.*,Delivery.OrderLines from Delivery.OrderDetails d where d.orderId in ['114156723']
Delivery is my bucket Name
Below is the document
Please help me in writing a query to get entire document
{
"OrderDetails": {
"orderId": "114156737",
"vanNumber": "5J",
"voucherPromotionName": "Computers for Schools",
"customerNumber": "85516242",
"shortOrderNumber": "4692",
"VoucherName": "Clubcard Voucher"
},
"OrderLines": {
"Product": [
{
"isApplicableForVat": "N",
"productQuantity": "6",
"productId": "52599951",
"productDescription": "Ni Pstrd S/Skimmed Milk 3ltr "
},
{
"isApplicableForVat": "Y",
"productQuantity": "1",
"productId": "55771771",
"productDescription": "Dale Farm Vanilla Ice Cream 1ltr *"
}
]
},
"DeliveryDetails": {
"deliverySlotStartTime": "20:00",
"deliverySlotEndTime": "21:00"
},
"ECoupons": {
"coupon": "0.0000"
},
"_class": "com.model.CustomerReceipt",
"OutOfStockProducts": {},
}
I tried with below 2 query but getting only OrderDetails details
select d.* from Delivery.OrderDetails d where d.orderId in ['114156723']
select d.*,Delivery.OrderLines from Delivery.OrderDetails d where d.orderId in ['114156723']
Delivery is my bucket Name
Thanks

Do this to get the full document:
select d from Delivery d where d.OrderDetails.orderId in ['114156723']

Related

Cosmos Db (need any sort of iteration mechanism)

want to check my document have same value in object A for eg:
{
"id": "1234-wrew-1234314"
"_ts": 1672840679
"A": [
{
"Id": "123",
"values": 167273168512
},
{
"Id": "1234",
"values": 1672731685
},
{
"Id": "123456",
"values": 1673461685
}
]
}
have this document now i want to check all values have same value or not is there any way to do this?
what i already tried :
select EXISTS(
SELECT VALUE n
FROM n IN c.A
WHERE c.A[0].values= c.A[1].values) as a
from c
where c.id ="1234-wrew-1234314"
its working fine if i have only 2 records in object A but i want generic solution to handle any number of records in object.
i also try with array_contain but its not working.
Thanks in advance.
Does this do what you need?
SELECT d.MaxValue = d.MinValue ? 'All the same' : 'Not the same'
FROM
(
SELECT MAX(a.values) AS MaxValue ,
MIN(a.values) AS MinValue
FROM c
JOIN a IN c.A
WHERE c.id = "1234-wrew-1234314"
) d

select non unique record based on a unique combination of 2 fields cosmos db

I have many records in Cosmos DB container with the following structure (sample):
{
"id": "aaaa",
"itemCode": "1234",
"itemDesc": "TEST",
"otherfileds": ""
}
{
"id": "bbbb",
"itemCode": "1234",
"itemDesc": "TEST2",
"otherfileds": ""
}
{
"id": "cccc",
"itemCode": "5678",
"itemDesc": "HELLO",
"otherfileds": ""
}
{
"id": "dddd",
"itemCode": "5678",
"itemDesc": "HELLO",
"otherfileds": ""
}
{
"id": "eeee",
"itemCode": "9012",
"itemDesc": "WORLD",
"otherfileds": ""
}
{
"id": "ffff",
"itemCode": "9012",
"itemDesc": "WORLD",
"otherfileds": ""
}
Now I want to select records from this where an item code have a non distinct item description. Based on the above example records, I would like to return item code 1234 since it has different values of item descriptions in other records.
{
"id": "aaaa",
"itemCode": "1234",
"itemDesc": "TEST",
"otherfileds": ""
}
{
"id": "bbbb",
"itemCode": "1234",
"itemDesc": "TEST2",
"otherfileds": ""
}
I have tried the below query, but realised, it will return the duplicate entries which has same item code and description only.
select count(1) from (select distinct value d.itemCode FROM (SELECT
c.itemCode, c.itemDesc, COUNT(1) as dupcount
FROM
c where c.itemCode<>null
GROUP BY
c.itemCode, c.itemDesc) d where d.dupcount>1 )
But I need to find records where the same item code is having different item descriptions (the query above will return only records which has more than one occurrence of item code/descriptions, ie, item code 9012 and 5678)
EDIT
I think i managed to form the query to filter these results by 2 sub queries (I think this could be improved though).
select e.itemCode from (select d.itemCode, count(1) as dupcount FROM
(SELECT
c.itemCode, c.itemDesc
FROM
c where c.itemCode<>null
GROUP BY
c.itemCode, c.itemDesc) d group by d.itemCode )e where e.dupcount>1
I think I managed to form the query to filter these results by 2 sub-queries (I think this could be improved though).
select distinct e.itemCode from (select d.itemCode, count(1) as dupcount FROM
(SELECT
c.itemCode, c.itemDesc
FROM
c where c.itemCode<>null
GROUP BY
c.itemCode, c.itemDesc) d group by d.itemCode )e where e.dupcount>1

Cosmos DB Query Array value using SQL

I have several JSON files with below structure in my cosmos DB.
[
{
"USA": {
"Applicable": "Yes",
"Location": {
"City": [
"San Jose",
"San Diego"
]
}
}
}]
I want to query all the results/files that has the array value of city = "San Diego".
I've tried the below sql queries
SELECT DISTINCT *
FROM c["USA"]["Location"]
WHERE ["City"] IN ('San Diego')
SELECT DISTINCT *
FROM c["USA"]["Location"]
WHERE ["City"] = 'San Diego'
SELECT c
FROM c JOIN d IN c["USA"]["Location"]
WHERE d["City"] = 'San Diego'
I'm getting the results as 0 - 0
You need to query data from your entire document, where your USA.Location.City array contains an item. For example:
SELECT *
FROM c
WHERE ARRAY_CONTAINS (c.USA.Location.City, "San Jose")
This will give you what you're trying to achieve.
Note: You have a slight anti-pattern in your schema, using "USA" as the key, which means you can't easily query all the location names. You should replace this with something like:
{
"Country": "USA",
"CountryMetadata": {
"Applicable": "Yes",
"Location": {
"City": [
"San Jose",
"San Diego"
]
}
}
}
This lets you query all the different countries. And the query above would then need only a slight change:
SELECT *
FROM c
WHERE c.Country = "USA
AND ARRAY_CONTAINS (c.CountryMetadata.Location.City, "San Jose")
Note that the query now works for any country, and you can pass in country value as a parameter (vs needing to hardcode the country name into the query because it's an actual key name).
Tl;dr don't put values as your keys.

Cosmos SQL where sessions[any].venues[any].id = this

Using this structure as an example, saved in a Cosmos database (course-database) in a collection (course-collection):
{
"courseId": "courseId",
"sessions": [
{
"sessionId": "sessionId1",
"venues": [
{
"id": "venueId1"
},
{
"id": "venueId2"
}
]
},
{
"sessionId": "sessionId2",
"venues": [
{
"id": "venueId3"
},
{
"id": "venueId4"
}
]
}
]
}
How do you create the SQL:
Count the total number of courses, where a course has at least one session, which has at least one venue, which has an ID equals to e.g. venueId3
I've got this so far, but it restricts to the first item of the list, as opposed to just any:
SELECT COUNT(c.id) FROM c WHERE c.sessions[0].venues[0].id = "id"
The answer was join:
SELECT COUNT(c.id)
FROM c
JOIN s in c.sessions
JOIN v in s.venues
WHERE CONTAINS(v.id,"venueId3")
You would then add a new join the deeper in the JSON you would want to go e.g. if venues had an array of contacts:
SELECT COUNT(c.id)
FROM c
JOIN s in c.sessions
JOIN v in s.venues
JOIN co in v.contacts
WHERE CONTAINS(co.id,"contactId")

Efficient text searching across multiple string fields in mongo using nodejs

There are like 10000 records like this that are stored in mongo. How to search the following efficiently in node.js. The search criteria can be based on any of the following 3 fields in mongo. The search criteria will come from front end and they can search userID or employeeName or department. For search only one input is going to be taken i.e only one field which can match any of these fields something like google search.
{
"userID": "01000900",
"employeeName": "A Abc",
"department": "Replenishment Boston"
},
{
"userID": "01001024",
"employeeName": "K Gbc",
"department": "Sales S-II - MA Core Urb"
},
{
"userID": "01001023",
"employeeName": "Ga Va",
"department": "Sales Phoenix"
},
{
"userID": "01000282",
"employeeName": "D Din",
"department": "Sales S-II - California - Me"
}
Assume searchText is the value which you are getting from client. Use regular expression.
{
$or:[
{userId:{$regex: searchText, $options: 'i'}},
{employeeName:{$regex: searchText, $options: 'i'}},
{department:{$regex: searchText, $options: 'i'}}
]
}
You can use Text index on all 3 fields like following:
db.collection.createIndex(
{
userID: "text",
employeeName: "text",
department: "text"
}
)
The query db.collection.find( { $text: { $search: "string from input" } } )
should do something like google search. Docs for text query syntax.
You can create index for each field. In this way the when you fins some data it will not scan the whole document. The search will be based on the index. So you will get the result instantaneously. Now for creating index. Suppose you want to create an index on employeeName then do the following:
db.collectionName.createIndex({employeeName:1});

Resources