How to take an array of docs as condition to query then join with other table? - rethinkdb

I have three tables hold these docs:
//user----tables:users
{
Id:"user/001"
Name:"Jack Losen"
....
}
//schooling---table:schoolings
{
Id:"schooling/001"
Name:""
Major:"",
EnrollYear:1983
}
//user_schooling-----join table:user_schooling
{
user_id:"user/001"
schooling_id:"schooling/001"
}
How to take an array of schoolings as condition to query user ids by join table:user_schooling?
for example:if I want to get all ids of the users who have been in A school at B year,or(not and) been in C school at D year...(or more),then my query condition may like this:
condition: [
{
Name:"A"
....
EnrollYear:B
},
{
Name:"C"
...
EnrollYear:D
}
]

Guys in rethinkdb mail list gave right answer:
r.table('user_schooling').filter(function(row) {
return r.table('schooling').get(row('SchoolingId')).do(function(school) {
return r.expr([{Name:"MASTER", EnrollTime:2003}]).pluck('Name', '
EnrollTime').contains(school.pluck('Name', 'EnrollTime'));
});
}).pluck("UserId")

Related

Performing A GraphQL Selection On A Union for shopify SellingPlanPricingPolicy

I am trying to get union data using the Graphql APIfor Shopify and am unable to find any documentation on selecting data in unions.
Here is my current query:
query subscriptionPlans {
sellingPlanGroup(id: 1234) {
id
name
sellingPlans(first: 1) {
edges {
node {
id
billingPolicy {
... on SellingPlanRecurringBillingPolicy {
interval
intervalCount
}
}
pricingPolicies {
on
SellingPlanRecurringPricingPolicy {
afterCycle
adjustmentType
adjustmentValue {
on
SellingPlanPricingPolicyPercentageValue {
percentage
}
on
MoneyV2 {
amount
currencyCode
}
}
}
}
}
}
}
}
}
I am able to retrieve the sellingPlans in this scenario. When I get to the SellingPlanPricingPolicy portion I run into issues. I am receiving the error:
Selections can't be made directly on unions (see selections on SellingPlanPricingPolicy)
However, I am unable to find any documentation on making selections using the documentation at: https://shopify.dev/docs/admin-api/graphql/reference/products-and-collections/sellingplanpricingpoli...
Any help with this would be appreciated. I just need an example of how to select this information.
Found the answer in https://graphql.org/learn/queries/#meta-fields. In case of sponsorEntity it looks like this:
sponsorEntity {
... on User {
name
}
}
The solution is to select the attributes on the respective sub-type of the union.

Is it possible to filter a collection based on a collection field on the content type in graphql & contentful

This might be impossible in graphql/contentful or introduce too much complexity but I'm trying to query a collection and filter on a collection field, something like the following...
query {
eventCollection(
where: {
OR: [
{ categoryCollection: { key: "fashion" } }
]
}
) {
items {
slug
}
}
My back up plan is to query all the events and filter in the client but I thought it would be possible to do the above.
Contentful DevRel here. 👋
Currently, that's not possible. But what you can do is flip the query around and filter on the categoryCollection and then use linkedFrom to request the items linking to it.
query {
categoryCollection(where: {
key: "fashion
}) {
items {
title
linkedFrom {
eventCollection {
items {
slug
}
}
}
}
}
}

How to query without using Eloquent?

I have these few tables:
issues
1. id
2. name
tags
1. id
2. name
issue_tag
1. issue_id
2. tag_id
images
1. id
2. url
3. issue_id
The relationship of these tables is stated below:
Issue hasMany Images, Images belongsTo Issue
Issues belongsToMany Tags, Tags belongsToMany Issues
How can I retrieve all the records by NOT USING ELOQUENT, just by using the QUERY BUILDER. I would like to retrieve the data in the format like:
[
{
id: issue_id,
name: issue_name,
tags: [
{
id: tag_id_1,
name: tag_name_1
},
{
id: tag_id_2,
name: tag_name_2
}
]
},
{
...
},
...
]
Please someone help me because I could not solve this problem for a long time, I can only solve it by using Eloquent. But using Eloquent is not a solution for me.
You can use join query. For Details follow this link demo
Or
Use DB:: statement ('your raw query here'). Hope this helps.
Use can Laravel DB::select(); function
With the use of select, we can pass Raw SQL query into select
$innerData = [];
$issueAll = DB::select('select i.id,i.name,i2.url as image,t.id as tag_id,
t.name as tag_name,it.issue_id from issues as i
inner join issue_tag as it on it.issue_id=i.id
inner join tags as t on it.tag_id = t.id
inner join images as i2 on i.id = i2.issue_id
');
foreach ($issueAll as $issue) {
$innerData[$issue->id]["id"] = $issue->id;
$innerData[$issue->id]["name"] = $issue->name;
if (isset($innerData[$issue->id]['tags'][$issue->issue_id])) {
array_push($innerData[$issue->id]['tags'], ['id'=>$issue->tag_id, 'name'=>$issue->tag_name]);
} else {
$innerData[$issue->id]['tags'][$issue->issue_id] = ['id'=>$issue->tag_id, 'name'=>$issue->tag_name];
}
}
return $innerData;
The response you get
{
"1":{
"id":1,
"name":"issue1",
"tags":{
"1":{
"id":1,
"name":"tag1"
},
"2":{
"id":1,
"name":"tag1"
}
}
},
"2":{
"id":2,
"name":"issue2",
"tags":{
"2":{
"id":1,
"name":"tag1"
}
}
}
}
Hope it helps.
In laravel if you don't want to use eloquent then you can use with DB::statement('your query') and don't forget to use USE DB; at the top of the file

updating an array of nested documents rethinkdb

I have a document schema like this:
{
"name":"",
"type":"",
"posts":[
{
"url":"",
"content":"",
...
},
{
"url":"",
"content":"",
...
}
...
]
}...
I forgot to create id's for each post on insertion in database. So i'm trying to create a query for that:
r.db('test').table('crawlerNovels').filter(function (x){
return x.keys().contains('chapters')
}).map(function (x){
return x('chapters')
}).map(
function(x){
return x.merge({id:r.uuid()})
}
)
instead this query return all posts with an id but doesn't actually update in the database. I tried using a forEach instead of a map function at the end this doesn't work
After lots of tweaking and frustration i figured it out:
r.db('test').table('crawlerNovels').filter(function (x){
return x.keys().contains('chapters')
}).update(function(novel){
return {"chapters":novel('chapters').map(
function(chapter){
return chapter.merge({"id":r.uuid()})
})}
},{nonAtomic:true})

order by in sub document in DocumentDB

I have a json document like following with sub-document
[
{
"id": "73e799df-b7a5-7470-4f25-ee6c1811a5b2",
"tblType": "search",
"memberId": 2,
"results": [
{"prcnt": 89,"distance": 8867775.747141607},
{"prcnt": 30,"distance": 11010216.470879028},
{"prcnt": 96,"distance": 9128590.716183286},
{"prcnt": 41,"distance": 9652043.937920697}
]
}
]
i want to get the 'results' tag data only in the query with order by prcnt
SELECT top 10 m.results FROM m join r in m.results where m.memberId=2
and m.tblType='search' order by r.prcnt
when i am executing the query, getting the error as follows..
Order-by over correlated collections is not supported.
how to get the data per my requirement.
Thanks in advance!!
According to your requirement, I have checked this issue. Here are my understanding about this issue:
If the memberId and tblType could locate the single document, then you could refer to this similar issue:
UDF
function sortByPrcntNumber (results) {
return results.sort(function(a,b){
return a.prcnt-b.prcnt;
});
}
QUERY
SELECT value udf.sortByPrcntNumber(c.results)
from c
where c.memberId=2 and c.tblType='search'
If the memberId and tblType could retrieve multiple documents, I assumed that you could leverage STORED PROCEDURES as follows:
function sample(top,tblType,memberId){
var collection=getContext().getCollection();
var isAccepted=collection.queryDocuments(
collection.getSelfLink(),
"SELECT value {\"prcnt\":r.prcnt,\"distance\":r.distance} from c join r in c.results where c.tblType='"+tblType+"' and c.memberId="+memberId+"",
function(err,feed,options){
if(err) throw err;
if(!feed||!feed.length) getContext().getResponse().setBody("no docs found");
else {
//order by prcnt
var orderedFeed=feed.sort(function(a,b){
return a.prcnt-b.prcnt;
});
//select top
var topFeed=orderedFeed.slice(0,top);
getContext().getResponse().setBody(topFeed);
}
});
if(!isAccepted) throw new Error("The query was not accepted by the server.");
}
Result

Resources