Home > OS >  Neo4j: Do queries on specific database?
Neo4j: Do queries on specific database?

Time:05-05

I´m new to Neo4j, and want to implement a service that makes use of it.

I´ve read the docs and searched for it, however I still didn´t get an answer to this simple question:

How do I specify which database to query in a Neo4j query?

E.g. I connected to bolt://localhost:7687, and have three databases in there: system, neo4j, and mydb. The neo4j database is the standard.

When I open the Neo4j browser and do a query such as MATCH (n) RETURN n, it automatically assumes that I want to query the standard DB which is called neo4j. However, I want to query another one, mydb. My output when I query aforementioned query says

{
  "query": {
    "text": "match (n) return n",
    "parameters": {}
  },
  "queryType": "r",
  "counters": {
    "_stats": {
      "nodesCreated": 0,
      "nodesDeleted": 0,
      "relationshipsCreated": 0,
      "relationshipsDeleted": 0,
      "propertiesSet": 0,
      "labelsAdded": 0,
      "labelsRemoved": 0,
      "indexesAdded": 0,
      "indexesRemoved": 0,
      "constraintsAdded": 0,
      "constraintsRemoved": 0
    },
    "_systemUpdates": 0
  },
  "updateStatistics": {
    "_stats": {
      "nodesCreated": 0,
      "nodesDeleted": 0,
      "relationshipsCreated": 0,
      "relationshipsDeleted": 0,
      "propertiesSet": 0,
      "labelsAdded": 0,
      "labelsRemoved": 0,
      "indexesAdded": 0,
      "indexesRemoved": 0,
      "constraintsAdded": 0,
      "constraintsRemoved": 0
    },
    "_systemUpdates": 0
  },
  "plan": false,
  "profile": false,
  "notifications": [],
  "server": {
    "address": "localhost:7687",
    "version": "Neo4j/4.4.5",
    "agent": "Neo4j/4.4.5",
    "protocolVersion": 4.4
  },
  "resultConsumedAfter": {
    "low": 2,
    "high": 0
  },
  "resultAvailableAfter": {
    "low": 8,
    "high": 0
  },
  "database": {
    "name": "neo4j"
  }
}

In the last JSON value is the proof that the query was executed on database neo4j.

What do I have to add to my queries to instead query another database in the same DBMS?

CodePudding user response:

You can change/specify the database using the following options.

  1. From the Neo4j Browser, you can select the database in the sidebar.

  2. In Cypher syntax, the use command lets you choose different databases. :use mydb.

  3. If you connect to Neo4j through an Application driver, you can specify the database while creating the session object.
    For example, if you are using the Python driver:

from neo4j import GraphDatabase
driver = GraphDatabase.driver(uri, auth=(user, password))
session = driver.session(database="mydb")
  1. Specify the default database in a system-wide manner by modifying the config_dbms.default_database value in the the neo4j.conf file.
  • Related