RethinkDB: Equivalent for "select where field not in (items)" - rethinkdb

I have a table that looks like this:
[
{ "name": "Alpha", "values": {
"someProperty": 1
}},
{ "name": "Beta", "values": {
"someProperty": 2
}},
{ "name": "Gamma", "values": {
"someProperty": 3
}}
]
I want to select all records where someProperty is not in some array of values (e.g., all records where someProperty not in [1, 2]). I want to get back complete records, not just the values of someProperty.
How should I do this with RethinkDB?

In python it would be:
table.filter(lambda doc: r.not(r.expr([1,2]).contains(doc["someProperty"]))
If the array comes from a subquery and you don't want to do it multiple times:
subquery.do(lambda array:
table.filter(lambda doc: r.not(array.contains(doc["someProperty"]))))

Related

Strapi Graphql query with filtering/conditional rendering

In my Strapi project, i'm trying to write a Graphql query that filters posts by an array of tags.
The post has to belong to all tags of the given array.
E.g. the post below would not meet the condition if given array is [1,2,3] but would pass the condition if array is [3,5].
Any help would be much appreciated
Example Post:
{
"id": "1",
"title": "Lorem Iopsum",
"created_at": "2021-02-19T22:53:19.204Z",
"tags": [
{
"id": "3",
"tag": "Porte De Gentily"
},
{
"id": "5",
"tag": "Bridges"
},
{
"id": "6",
"tag": "Towers"
}
]
}
I was trying something like:
query {
posts( where: {tags: {id_contains: [3,1]}}) {
title
tags{id},
created_at
}
}
You can do it something like this
query($ids:[ID]) {
posts( where: {tags: {id:$ids}}) {
title
tags{id},
created_at
}
}
And pass your ids as a query variable:
{
"ids": [1,5]
}

Laravel join as array for json response

maybe you can help me find out a query. I do a query Like
$Customers = Customer::paginate();
return response()->json([$Customers]);
this returns (just part of response)
[
{
"current_page": 1,
"data": [{"id": 1,}]
}
]
If I do join with other table all data is just put into "data" output. What I want is this for my join: How to get this output??
[
{
"current_page": 1,
"data": [{
"id": 1,
"join": [{ "join_column" : 1 }],
}]
}
]

ReferenceManyFields (One to Many Relationship)

I am working on a project where I have to create one to many relationships which will get all the list of records referenced by id in another table and I have to display all the selected data in the multi-select field (selectArrayInput). Please help me out in this, if you help with an example that would be great.
Thanks in advance.
Example:
district
id name
1 A
2 B
3 C
block
id district_id name
1 1 ABC
2 1 XYZ
3 2 DEF
I am using https://github.com/Steams/ra-data-hasura-graphql hasura-graphql dataprovider for my application.
You're likely looking for "nested object queries" (see: https://hasura.io/docs/1.0/graphql/manual/queries/nested-object-queries.html#nested-object-queries)
An example...
query MyQuery {
district(where: {id: {_eq: 1}}) {
id
name
blocks {
id
name
}
}
}
result:
{
"data": {
"district": [
{
"id": 1,
"name": "A",
"blocks": [
{
"id": 1,
"name": "ABC"
},
{
"id": 2,
"name": "XYZ"
}
]
}
]
}
}
Or...
query MyQuery2 {
block(where: {district: {name: {_eq: "A"}}}) {
id
name
district {
id
name
}
}
}
result:
{
"data": {
"block": [
{
"id": 1,
"name": "ABC",
"district": {
"id": 1,
"name": "A"
}
},
{
"id": 2,
"name": "XYZ",
"district": {
"id": 1,
"name": "A"
}
}
]
}
}
Setting up the tables this way...
blocks:
districts:
Aside: I recommend using plural table names as they are more standard, "districts" and "blocks"

RxJS distinct on array of objects in property

i am having an observable which returns something like this:
"#odata.context": "here is the context URL",
"value": [
{
"Id": 1,
"Value": "A"
},
{
"Id": 2,
"Value": "B"
},
{
"Id": 3,
"Value": "C"
},
{
"Id": 4,
"Value": "A"
}
]
}
Using RxJS i would like to get into the "value" property and use distinct on it to limit the response to something like this:
"#odata.context": "here is the context URL",
"value": [
{
"Id": 1,
"Value": "A"
},
{
"Id": 2,
"Value": "B"
},
{
"Id": 3,
"Value": "C"
}
]
}
could you help me with it?
It pretty much depends on how exactly you want to use this RxJS chain but I hope this leads you the right direction:
const source = of(data)
.pipe(
mergeMap(d => d.value),
distinct(v => v.Value),
toArray(),
)
.subscribe(console.log);
mergeMap will reiterate the array of values as separate emissions where distinct will ignore duplicate Values. Then toArray() will just collect all results.
Live demo: https://stackblitz.com/edit/rxjs-is1zp4

Elasticsearch to return documents based on 2 criteria where one is based on the other

I have documents in the following format:
{
"id": number
"chefId: number
"name": String,
"ingredients": List<String>,
"isSpecial": boolean
}
Here is a list of 5 documents:
{
"id": 1,
"chefId": 1,
"name": "Roasted Potatoes",
"ingredients": ["Potato", "Onion", "Oil", "Salt"],
"isSpecial": false
},
{
"id": 2,
"chefId": 1,
"name": "Dauphinoise potatoes",
"ingredients": ["Potato", "Garlic", "Cream", "Salt"],
"isSpecial": true
},
{
"id": 3,
"chefId": 2,
"name": "Boiled Potatoes",
"ingredients": ["Potato", "Salt"],
"isSpecial": true
},
{
"id": 4,
"chefId": 3
"name": "Mashed Potatoes",
"ingredients": ["Potato", "Butter", "Milk"],
"isSpecial": false
},
{
"id": 5,
"chefId": 4
"name": "Hash Browns",
"ingredients": ["Potato", "Onion", "Egg"],
"isSpecial": false
}
I will be doing a search where "Potatoes" is contained in the name field. Like this:
{
"query": {
"wildcard": {
"status": {
"value": "*Potatoes*"
}
}
}
}
But I also want to add some extra criteria when returning documents:
If the ingredients contain onion or milk, then return the documents. So documents with the id 1 and 4 will be returned. Note that this means that we have documents returned where chef ids are 1 and 3.
Then, for the documents where we haven't already got another document with the same chef id, return where the isSpecial flag is set to true. So only document 3 will be returned. 2 wouldn't be returned as we already have a document where the chef id is equal to one.
Is it possible to do this kind of chaining in Elasticsearch? I would like to be able to do this in a single query so that I can avoid adding logic to my (Java) code.
You can't have that sort of logic in one elasticsearch query. You could have a tricky query with aggregations / post_filter and so to have all the data you need in one query and then transform it in your Java application.
But the best approach (and the more maintainable) is to have two queries.

Resources