cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to conveniently get bool value back from cypher query through driver api?

lingvisa
Graph Fellow

For example:

with driver.session() as session:
        model_exists = session.run("""CALL gds.beta.model.exists('nc-model') YIELD exists""")
        for model_exist in model_exists:
            model_exists = model_exist[0]
            break

        if model_exists:
            print("Model  exists, which have to be dropped first!")
            session.run("""CALL gds.beta.model.drop('nc-model')""")

        print('Model dropped.')

In this code, is there a better way to get the single bool value out from the session's query, instead of doing this:

for model_exist in model_exists:
            model_exists = model_exist[0]
            break
1 ACCEPTED SOLUTION

Did you see the single()[0] at the end of the row?

View solution in original post

3 REPLIES 3

Hello @lingvisa

with driver.session() as session:
    model_exists = session.run("CALL gds.beta.model.exists('nc-model') YIELD exists").single()[0]
    if model_exists:
        print("Model exists, which have to be dropped first!")
        session.run("CALL gds.beta.model.drop('nc-model')")

    print('Model dropped.')

Regards,
Cobra

@Cobra , I tested, if you simply use 'if model_exists' without using the for loop to extract the bool value, the if checking always returns true, which is incorrect. 'Model_exists' is a Result object, not a bool. Could you test it?

Did you see the single()[0] at the end of the row?