Exporting result to csv in the middle of a cypher query - postgresql

I'm trying to export the results of a query to csv and still re-use the values from the query result as inputs for the second stage of the query.
I submit an initial query in my postgres database and pass the results to neo4j using apoc.load.jdbc. I've been trying to export the results for this postgres query using apoc.export.csv.query and then use values from the postgres query as search criteria for my cypher query.
This query works without any attempt to write to csv:
CALL apoc.load.jdbc('postgresql_url', 'SELECT paper_id FROM papers LIMIT 1') YIELD row
MATCH (n:paper)<-[r:REFERENCES]-(m:paper) WHERE n.paper_id = row.paper_id
RETURN n, m
Ideally, I'd like a query that does something likes this:
CALL apoc.load.jdbc('postgresql_url', 'SELECT paper_id FROM papers LIMIT 1') YIELD row
CALL apoc.export.csv.query('row', 'export/degree0.csv', {}) YIELD row
MATCH (n:paper)<-[r:REFERENCES]-(m:paper) WHERE n.paper_id = row.paper_id
RETURN n, m
This query returns and error that it row.paper_id is not defined.
I'm trying to get the results of the postgres query written to a csv and use data from that query result in the cypher query.

it is possible you are overriding the row variable.
you can try removing the 'YIELD row' from the export.csv in the second line.
if it doesn't work I would suggest removing also the match line and just Return *, so you can see exactly what the row variable contains.

Related

PDI Step MongoDB Input - $match using value gotten from a MySQL query in a previous step

My transformation starts with a Table input step with a query that retrieves a value from a table column (named ID_EXECUCAO).
I want to search my MongoDB Collection filtering by this value, using the function $match on my MongoDB input step query.
I know I can search by a parameter using "${PARAMETER}", but how do I search by a value passed from a previous step?
I tried this way and it doesn't work:
$match: {
"IdExecucao": $ID_EXECUCAO
}
I sure that you can't do it by get data from previous step like 'Table Input Step'. No option 'Insert data from step' in MongoDB Input.
As you known, the only way is to use Variable (${VARIABLE_NAME}).

Postgres/jOOQ replace jsonb[] element

I'm having a Spring application with jOOQ and Postgresql database having a table (issues) with the following two columns:
id (Long)
documents (jsonb[]) <- array of jsonb (not jsonb array)
The document json structure is on the following format:
{
"id": (UUID),
"name": (String),
"owner"; (String)
}
What I want to achieve is to be able to replace documents with matching id (normally only one) with a new document. I'm struggling with the jOOQ or even the plain SQL.
I guess I need to write some plain SQL in jOOQ to be able to do this but that is ok (to a minimum). I had an idea to do the following:
Unnest the document column
Filter out the document that should be updated of the array
Append the document that should be updated
Store the whole array
Raw SQL looks like this but missing the new document to be added:
UPDATE issues SET documents = (SELECT ARRAY_AGG(doc) FROM issues, UNNEST(issues.documents) AS doc WHERE doc->>'id' != 'e4e3422f-83a4-493b-8bf9-37980d532538') WHERE issues.id = 1;
My final goal is to write this in jOOQ and append the document to be replaced. I'm using jOOQ 3.11.4.
You should be able to just concatenate arrays in PostgreSQL:
UPDATE issues
SET documents = (
SELECT ARRAY_AGG(doc) || '{"id":"e4e3422f-83a4-493b-8bf9-37980d532538","name":"n"}'::jsonb
FROM issues, UNNEST(issues.documents) AS doc
WHERE doc->>'id' != 'e4e3422f-83a4-493b-8bf9-37980d532538'
)
WHERE issues.id = 1
Some common array functions will be added to jOOQ in the near future, e.g. array concatenation, but you can get away for now with plain SQL templating I suspect?

Query jsonb column in Postgres against multiple values

I have a column 'carInfo' of type jsonbin my PostgreSQL database and I want to be able to query against multiple values. I want the query to return only those rows that match all criteria. For example, if I had multiple columns, I would write the query like this:
select *
from car
where name = 'BMW'
AND 'year' = 2020
Since my column is of type jsonb I can make use of the containment operator (#>) like this:
select *
from car
where carInfo #> cast('{"name":"BMW"}') as jsonb
AND carInfo #> cast('{"year":"2020"}')
but I want the query to be dynamic, so that the user can query by any attributes they want.
So, they will send a list of search terms (in this case, the elements of that list would be {"name":"BMW"} and {"year":"2020"}.
Assuming, that I have the list as above, how would the query look like if I wanted to achieve the same result as when using the AND operator?
I tried it like this:
select *
from car
where carInfo #> any(array['{"name":"BMW"}', '{"year":"2020"}']::jsonb[])
but it acts the same way as when using the OR operator. I need to find those rows that contain BOTH the search terms
You could use #> ALL, but this should be better:
WHERE carInfo #> cast('{"name":"BMW", "year": "2020"}' as jsonb)

Using MongoDB to query selected field

I am trying to query out the data from my MongoDB database but there are some fields which I would like to omit as MongoDB will query the whole collections with id, n out.
I did this to limit the query but unfortunately only one field could be omitted but not the other which is the 'n' field. How can I omit two fields?
data = collection.find_one({"files_id": file_id},{"_id":0,"data":1})
And I also realized that my query for data has the field name (u'data') too, how can I query it so that it only returns the data? for this case it's a binary data
Example:
{u'data': Binary('\x00\x00\xed\x00\n\x00\x00\xd5\xa9\x00\x000\x00\x00\x00#\x00\x00\x0f\xff\xf0\x00\x0b\x80\x00\x00\x00
Kindly assist thanks!

MongoDB: How to execute a query to result of another query (nested queries)?

I need to apply a set of filters (queries) to a collection. By default, the MongoDB applies AND operator to all queries submitted to find function. Instead of whole AND I need to apply each query sequentially (one by one). That is, I need to run the first-query and get a set of documents, run the second-query to result of first-query, and so on.
Is this Possible?
db.list.find({..q1..}).find({..q2..}).find({..q3..});
Instead Of:
db.list.find({..q1..}, {..q2..}, {..q3..});
Why do I need this?
Bcoz, the second-query needs to apply an aggregate function to result of first-query, instead of applying the aggregate to whole collection.
Yes this is possible in MongoDB. You can write nested queries as per the requirement.Even in my application I created nested MongoDb queries.If you are familiar with SQL syntax then compare this with in of sql syntax:
select cname from table where cid in (select .....)
In the same way you can create nested MongoDB queries on different collections also.