N1ql to Get data from collection based on field value - n1ql

I have a document as below
{
"GMParcelStatus": {
"storeNumber": 5678,
"GMVehicleTrips": {
"GMVehicleTrip": [
{
"GMVehicleTripId": "1000101",
"MultiChannelOrders": {
"MultiChannelOrder": [
{
"multiChannelOrderID": "4BQGBNJ3U",
"multichannelParcels": [
{
"multiChannelParcelStatus": "LOADING_MISSING",
"UPI": "00000008101058629797"
},
{
"multiChannelParcelStatus": "OUTFORDELIVERY",
"UPI": "00000008101058684938"
}
]
},
{
"multiChannelOrderID": "4BQGUNY56W",
"multichannelParcels": [
{
"multiChannelParcelStatus": "DELIVERED",
"UPI": "00000008101058629793"
},
{
"multiChannelParcelStatus": "DELIVERED",
"UPI": "00000008101058684932"
}
]
}
]
}
}
]
}
},
"_class": "com.tesco.bean.MultiChannelParcelRequestVO"
}
I want to get all the document in my bucket data based on storeNumber and GMVehicleTripId.
I have 4 document similar to above with different GMVehicleTripId.
I have written N1ql query like below Select d.* from Delivery d JOIN Delivery.GMParcelStatus.GMVehicleTrips.GMVehicleTrip[0] b
on keys meta(d).id where b.GMVehicleTripId in ['1000101']
but i don't want to do this GMVehicleTrip[0].
please get me the right way to do.
Thanks,
Vinay J

SELECT d.* FROM Delivery d JOIN Delivery b ON KEYS meta(d).id
WHERE ANY v IN b.GMParcelStatus.GMVehicleTrips.GMVehicleTrip SATISFIES v.GMVehicleTripId IN ['1000101'] END;

Related

Fetch key value from json data using terraform

I have a json data file from which I need to fetch only the id attribute value into a separate list variable. I tried for loop but not able to get the required data using terraform. Can someone tell how to fetch the id part ? your help is much appreacited.
code:
locals {
data = jsondecode(file("./data.json"))[*]
sections = [ for item in local.data : item ]
}
output "ids" {
value = [for a in local.sections[0]: a]
}
json file:
{
"c":[
{
"id":"6",
"key":"c",
"name":"s01"
}
],
"l":{
"id":"7",
"key":"l",
"name":"s02"
},
"m":{
"id":"8",
"key":"mp",
"name":"s03"
},
"n":{
"id":"5",
"key":"cn",
"name":"s04"
},
"od":"odk",
"s":{
"id":"9",
"key":"cs",
"name":"s05"
},
"ss":{
"id":"1",
"key":"ss",
"name":"s06"
},
"in":{
"id":"65",
"key":"cn",
"name":"s07"
},
"b":{
"id":"2",
"key":"cb",
"name":"s08"
}
}
Not sure if "od":"odk", is mistake or not, but you can do the following:
locals {
data = jsondecode(file("./data.json"))
}
output "ids" {
value = [for v in values(local.data): v.id if can(v.id)]
}

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")

How to match and join results between two resolvers in one graphql query?

I have two resolver.
The one is Company resolve that return the company details like id, name and list of documents ids, like this example:
{
"data": {
"companyOne": {
"name": "twitter",
"documents": [
"5c6c0213f0fa854bd7d4a38c",
"5c6c02948e0001a16529a1a1",
"5c6c02ee7e76c12075850119",
"5c6ef2ddd16e19889ffaffd0",
"5c72fb723ebf7b2881679ced",
"5c753d1c2e080fa4a2f86c87",
...
]
}
}
}
And the another resolver gets me all the details of documents like this example:
{
"data": {
"documentsMany": [{
"name": "doc1",
"_id": 5c6c0213f0fa854bd7d4a38c,
}, {
"name": "doc2",
"_id": 5c6c02948e0001a16529a1a1,
},
...
]
}
}
How to match every data.companyOne.documents[id] to data.documentsMany[..]._id? in the query level? is it possible to do this graphql?
The expect results should be when I run the companyOne query (without change the code - just in the query level) it's should return with documents as object instead of array of string ids.
maybe something like?
query {
companyOne {
name,
documents on documentsMany where _id is ___???
}
}

how can i retrieve all the childrens by n1ql query

I am planning to retrieve the child elements all the parent, How can i retrieve it, being inside the array of array objects.
SELECT sd.*
FROM test AS t
UNNEST stateDetails AS sd;
{
"type":"countries",
"docName":"CountryData",
"countryDetails":[
{
"name":"US",
"code":"+1",
"stateInfo":[
{
"name":"Florida",
"id":"1212"
},
{
"name":"NewYork",
"id":"1214"
}
]
},
{
"name":"France",
"code":"+33",
"stateInfo":[
{
"name":"Grand Est",
"id":"5212"
},
{
"name":"Brittany",
"id":"5214"
}
]
}
]
}
I am expecting the following output to bring out the state details of all the countries with their respective country Name
[
{
"countryName":"US",
"name":"Florida",
"id":"1212"
},
{
"countryName":"US",
"name":"NewYork",
"id":"1214"
},
{
"countryName":"France",
"name":"Grand Est",
"id":"5212"
},
{
"countryName":"France",
"name":"Brittany",
"id":"5214"
}
]
Use double UNNEST and project what you need
SELECT cd.name AS countryName, sd.name, sd.id
FROM test AS t
UNNEST t.countryDetails AS cd
UNNEST cd.stateInfo AS sd
WHERE t.type = "countries";

Mongo text search with AND operation for multiple words partially enered

{
TypeList" : [
{
"TypeName" : "Carrier"
},
{
"TypeName" : "Not a Channel Member"
},
{
"TypeName" : "Service Provider"
}
]
}
Question :
db.supplies.find("text", {search:"\"chann\" \"mem\""})
For above query I want display :
{
TypeName" : "Not a Channel Member"
}
But I am unable to get my result.
What are changes I have to do in query .
Please help me.
The below query will return your desired result.
db.supplies.aggregate([
{$unwind:"$TypeList"},
{$match:{"TypeList.TypeName":{$regex:/.*chann.*mem.*/,$options:"i"}}},
{$project:{_id:0, TypeName:"$TypeList.TypeName"}}
])
If you can accept to get an output like this:
{
"TypeList" : [
{
"TypeName" : "Not a Channel Member"
}
]
}
then you can get around using the aggregation framework which generally helps performance by running the following query:
db.supplies.find(
{
"TypeList.TypeName": /chann.*mem/i
},
{ // project the list in the following way
"_id": 0, // do not include the "_id" field in the output
"TypeList": { // only include the items from the TypeList array...
$elemMatch: { //... where
"TypeName": /chann.*mem/i // the "TypeName" field matches the regular expression
}
}
})
Also see this link: Retrieve only the queried element in an object array in MongoDB collection

Resources