RethinkDB: join table with array of keys - rethinkdb

I have table users like
[
{
"id": "1" ,
"name": "John",
"groups": ["1", "2"]
}
{
"id": "2" ,
"name": "Jack",
"groups": ["1"]
}
]
And table groups like
[
{
"id": "1" ,
"name": "Admin",
"description": "Administrator"
},
{
"id": "2" ,
"name": "Guest",
"description": "Dear guest"
}
]
How can I make ReQL selection query with results like
[
{
"id": "1" ,
"name": "John",
"groups": ["Administrator", "Guest"]
},
{
"id": "2" ,
"name": "Jack",
"groups": ["Administrator"]
}
]
?
and
[
{
"id": "1" ,
"name": "John",
"groups": [
{
"id": "1" ,
"name": "Admin",
"description": "Administrator"
},
{
"id": "2" ,
"name": "Guest",
"description": "Dear guest"
}
]
},
{
"id": "2" ,
"name": "Jack",
"groups": [
{
"id": "2" ,
"name": "Guest",
"description": "Dear guest"
}
]
}
]

Assuming Javascript API and by using .merge() command:
r.table("users").merge(function(user) {
return {
groups: r.table("groups")
.getAll(r.args(user("groups")))
.getField("name")
.coerceTo("array")
};
})
and
r.table("users").merge(function(user) {
return {
groups: r.table("groups")
.getAll(r.args(user("groups")))
.coerceTo("array")
};
})

Related

Elastic search query - all objects of nested array should have specific value

I want to write Elasticsearch query where I am able to look into a nested array, and check a key for all those elements inside the array, if all of them have that value then the root document should be selected.
this is a sample data :
[
{
"_index": "allproperties",
"_type": "_doc",
"_id": "5241050",
"_score": 0.0,
"_source": {
"id": 5241050,
"type": "HOUSE",
"state": "NSW",
"agency": {
"id": 31,
"code": "AU_LNT",
"name": "Luthor Properties",
"config": "{}"
},
"branch": {
"id": 89,
"name": "Luthor Sales Unit Trust",
"tradingName": "Luthor Properties",
"internalCode": "AU_LNT_G"
},
"images": null,
"leases": [
{
"id": 26439,
"bond": 2000,
"leased": true,
"source": "PORTAL",
"status": "SIGNED",
"endDate": "2022-11-06T00:00:00",
"tenants": [
{
"id": 11106,
"role": "TENANT",
"user": {
"id": 38817,
"dob": "1900-01-01",
"name": "Liam",
"email": "tempo#tempo.com",
"phone": "+61400000000",
"surname": "Tempo",
"salutation": "Mr.",
"invitations": null,
"lastInAppActivity": null
},
"address": "45675 Bruce Hwy, Coolbie QLD 4850, Australia",
"extendedData": "{\"rentSplitAmount\":22.62}",
"roleAdjective": "PRIMARY",
"addressComponents": {
"state": "QLD",
"suburb": "Coolbie",
"country": "Australia",
"postcode": "4850",
"streetName": "Bruce Hwy",
"unitNumber": null,
"poboxNumber": null,
"streetNumber": "45675",
"addressComponentsUnavailable": null
}
}
],
"signDate": "2022-10-05T04:58:08.887",
"startDate": "2022-10-06T00:00:00",
"moveInDate": "2022-10-06T00:00:00",
"appointments": [
{
"id": 3506,
"type": "REMOTE",
"date_time": "2022-10-12T04:56:00+00:00",
"createdBy": "Lex Luthor",
"createdDate": "2022-10-05T04:56:52.936",
"lastModifiedBy": "Lex Luthor",
"lastModifiedDate": "2022-10-05T04:56:53.51"
}
],
"createdDate": "2022-10-05T04:55:42.247",
"rentalAmount": 500,
"dateAvailable": null,
"extendedData": {
"last_rent_adjustment_date": 1665014400000
},
"leaseDuration": 32,
"rentalFrequency": "2",
"signedManually": false
}
],
"refId": "b66326eb-a6b2-42b6-b058-b46847e13399",
"source": "PT",
"status": null,
"suburb": "Hurstville",
"address": "456 Forest Road Hurstville NSW AUSTRALIA 2220",
"country": "Australia",
"bedrooms": 3,
"category": null,
"pmsCode": "84c34d15-a0ab-4791-b9e3-1bdac215b99c",
"postcode": "2220",
"agencyId": 31,
"bathrooms": 3,
"branchId": 89,
"carspaces": 3,
"createdBy": "system",
"streetname": "Forest Road",
"unitnumber": null,
"description": null,
"createdDate": "2022-10-05T04:51:12.619",
"entitlements": [
{
"id": 3453799,
"role": "LANDLORD",
"user": {
"id": 22855,
"dob": null,
"name": "please enter ownership name",
"email": "mag.m#rr.com.au",
"phone": "0400000000",
"surname": "",
"salutation": null,
"lastInAppActivity": null
},
"company": {
"id": 137,
"abn": "",
"acn": "",
"phone": "0400000000",
"address": "1234 Park Avenue New York NY USA 10037-1702",
"companyName": "please enter ownership name",
"displayName": "please enter ownership name",
"email": "mag.m#rr.com.au"
},
"roleAdjective": "GROUP"
},
{
"id": 3453800,
"role": "AGENT",
"user": {
"id": 20054,
"dob": null,
"name": "Paul",
"email": "p.b#mr.com",
"phone": "+61403084232",
"surname": "Botti",
"salutation": null,
"lastInAppActivity": null
},
"company": null,
"roleAdjective": "MANAGING"
},
{
"id": 3453801,
"role": "AGENT",
"user": {
"id": 20054,
"dob": null,
"name": "Paul",
"email": "p.b#mr.com",
"phone": "+61403084232",
"surname": "Botti",
"salutation": null,
"lastInAppActivity": null
},
"company": null,
"roleAdjective": "LEASING"
}
],
"streetnumber": "456",
"advertised": false,
"commission_type": null,
"lastModifiedBy": "system",
"lastModifiedDate": "2022-10-05T04:58:09.043",
"_meta": {
"branches": {
"id": [
89
]
},
"agencies": {
"id": [
31
]
},
"user_property_entitlement": {
"id": [
3453799,
3453800,
3453801
]
},
"users": {
"id": [
20054,
22855,
38817
]
},
"companies": {
"id": [
137
]
},
"leases": {
"id": [
26439
]
},
"user_lease_entitlement": {
"id": [
11106
]
},
"appointments": {
"id": [
3506
]
}
}
},
"sort": [
1664945472619,
0.0
]
}
]
In this particular case, leases is a nested document, I want to select the documents where all the leases are in cancelled state or leases is null, i.e; either all of the objects inside leases should have status as CANCELLED or the leases key should be null.
Already went through this question, but did'nt quite get it as its an old answer from 2015 and methods used in this got deprecated.
Try this query:
GET idx_test/_search?filter_path=hits.hits
{
"query": {
"bool": {
"must_not": [
{
"nested": {
"path": "leases",
"query": {
"bool": {
"must_not": [
{
"bool": {
"should": [
{
"term": {
"leases.status.keyword": "CANCELLED"
}
},
{
"bool": {
"must_not": [
{
"exists": {
"field": "leases"
}
}
]
}
}
]
}
}
]
}
}
}
}
]
}
}
}

How to insert the element in to list in shell

{
"id": "123",
"teamName": "Developers",
"members": [
{
"name": "E",
"fullName": null,
"type": "ROLE",
"roleId": ""
},
{
"name": "Managers",
"fullName": null,
"type": "ROLE",
"roleId": ""
}
],
"permissions": [
"template#edit",
"trigger#view_trigger",
"release#start",
"release#reassign_task",
"template#view",
"folder#view",
"template#create_release",
"release#abort",
"folder#edit",
"release#view"
],
"systemTeam": false
}
I have this above json output from curl, want to insert another element in to that members block, see below, added the second element whose name key is F.
Expected output:
{
"id": "123",
"teamName": "Developers",
"members": [
{
"name": "E",
"fullName": null,
"type": "ROLE",
"roleId": ""
},
{
"name": "F",
"fullName": null,
"type": "ROLE",
"roleId": ""
},
{
"name": "Managers",
"fullName": null,
"type": "ROLE",
"roleId": ""
}
],
"permissions": [
"template#edit",
"trigger#view_trigger",
"release#start",
"release#reassign_task",
"template#view",
"folder#view",
"template#create_release",
"release#abort",
"folder#edit",
"release#view"
],
"systemTeam": false
},
{
"id": "123",
"teamName": "Managers",
"members": [
{
"name": "E",
"fullName": null,
"type": "ROLE",
"roleId": ""
},
{
"name": "F",
"fullName": null,
"type": "ROLE",
"roleId": ""
},
{
"name": "Managers",
"fullName": null,
"type": "ROLE",
"roleId": ""
}
],
"permissions": [
"template#edit",
"trigger#view_trigger",
"release#start",
"release#reassign_task",
"template#view",
"folder#view",
"template#create_release",
"release#abort",
"folder#edit",
"release#view"
],
"systemTeam": false
}
}
I want to do this with in sh script, tried doing find and replace, but looks ugly.

How to use filter in map

I have following code:
%dw 2.0 output application/json var attributeIdMapping =
${vault::attributeIdMapping}
--- {
"objectTypeId": 3,
"attributes": vars.requestBody mapObject (value, key) ->
{
"objectTypeAttributeId": (attributeIdMapping.attributes filter ($.name == key))[0].id,
"objectAttributeValues": [{
"value": value,
"key": key
}]
} }
attributeIdMapping:
{
"attributes": [
{
"name": "accountType",
"id": "87"
},
{
"name": "accountClass",
"id": "89"
},
{
"name": "accountName",
"id": "85"
},
{
"name": "displayName",
"id": "18"
},
{
"name": "accountCategory",
"id": "88"
},
{
"name": "accountNumber",
"id": "84"
},
{
"name": "description",
"id": "86"
},
{
"name": "accountGroup",
"id": "90"
}
]
}
vars.requestBody:
{
"displayName": "TestMulesoft2",
"description": "Test"
}
But my filter shows null in the end. As I understand key is not passed to level below map itself. How can I get this working?
I put all the input data inside the script to simplify reproduction. The problem seems to be using the operator == to compare a string and a key types returns empty. Using the operator ~= which attempts to coerce appears to return the expected result:
%dw 2.0
output application/json
var attributeIdMapping =
{
"attributes": [
{
"name": "accountType",
"id": "87"
},
{
"name": "accountClass",
"id": "89"
},
{
"name": "accountName",
"id": "85"
},
{
"name": "displayName",
"id": "18"
},
{
"name": "accountCategory",
"id": "88"
},
{
"name": "accountNumber",
"id": "84"
},
{
"name": "description",
"id": "86"
},
{
"name": "accountGroup",
"id": "90"
}
]
}
var requestBody = {
"displayName": "TestMulesoft2",
"description": "Test"
}
--- {
"objectTypeId": 3,
"attributes": requestBody mapObject (value, key) ->
{
"objectTypeAttributeId": (attributeIdMapping.attributes filter ($.name ~= key))[0].id,
"objectAttributeValues": [{
"value": value,
"key": key
}]
} }
Output:
{
"objectTypeId": 3,
"attributes": {
"objectTypeAttributeId": "18",
"objectAttributeValues": [
{
"value": "TestMulesoft2",
"key": "displayName"
}
],
"objectTypeAttributeId": "86",
"objectAttributeValues": [
{
"value": "Test",
"key": "description"
}
]
}
}

ruby extract data from nested array

How I can access to a depth key value from a nested hash?
I want to extract values inside of _Data, I want to extract the field and size inside data[], to be printed like fieldvalue_sizevalue
elemento3_3
I have this code but only get the all the data keys and values and i had some troubles making this iteration, I am new at ruby
_Data[0][:checks].each do |intera|
puts "#{itera[:data]}
end
this is the nested array
_Data =[
{
"agent": "",
"bottom_comments": [],
"checks": [
{
"title": "new",
"data": [{
"field": "elemento1",
"value": "0",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento2",
"value": "0",
"size":"4",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento3",
"value": "0",
"size":"3",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento4",
"value": "0",
"size":"17",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento5",
"value": "0",
"size":"12",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento6",
"value": "0",
"size":"19",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento7",
"value": "0",
"size":"9",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento8",
"value": "0",
"size":"10",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento11",
"value": "0",
"size":"14",
}
]
},
{
"title": "new",
"data": [{
"field": "elemento19",
"value": "0",
"size":"14",
}
]
},
{
"type": "condiciones",
"elementos": [
{
"table": [
{
"title": "",
"name": "radio",
},
{
"title": "",
"name": "xenon",
},
{
"title": "",
"name": "aluminio",
},
{
"title": "",
"name": "boro",
},
{
"title": "",
"name": "oro",
},
{
"title": "",
"name": "bromo",
},
{
"title": "",
"name": "oxigeno",
}
]
}
]
}
]
}
]
_Data[0][:checks].each do |intera|
intera[:data].each do |data|
puts "#{data[:filed]}_#{data[:value]}"
end
end
Hope this may help you.
Here is one line code to print the values if they exist. Let me know if any further details on any of the methods used will be helpful.
_Data[0][:checks].pluck(:data).flatten.compact.each{|i| puts "#{i[:field]}_#{i[:size]}" if i[:field] && i[:size]}

Vuetify's v-select doesn't show all the items for some reason

This is extremely weird for me - my code is very basic:
<v-select
chips
multiple
:items="areas"
v-model="person.applicant.areas"
:label="trans('Areas of interest')"
item-text="name"
item-value="id"
></v-select>
The items when echoed right before that with {{areas}} returns as follows:
[
{ "interestsAreas": "TEst", "id": 0, "name": "TEst" },
{ "interestsAreas": "Test2", "id": 1, "name": "Test2" },
{ "interestsAreas": "Something", "id": 0, "name": "Something" },
{ "interestsAreas": "1", "id": 1, "name": "1" },
{ "interestsAreas": "2", "id": 2, "name": "2" },
{ "interestsAreas": "3", "id": 3, "name": "3" }
]
Yet, whatever I do the v-select drops "Something" and "1" out of the dropdown even if I rename them or whatever.
I can't catch the logic behind dropping some of them, is there any known bug or something that I am not doing right in the invocation?
I am guessing it has something to do with the item-value, as id needs to be unique
[
{ "interestsAreas": "TEst", "id": 0, "name": "TEst" },
{ "interestsAreas": "Test2", "id": 1, "name": "Test2" },
{ "interestsAreas": "Something", "id": 0, "name": "Something" },// the id value is already used so try changing it
{ "interestsAreas": "1", "id": 1, "name": "1" },//same for this one as well
{ "interestsAreas": "2", "id": 2, "name": "2" },
{ "interestsAreas": "3", "id": 3, "name": "3" }
]

Resources