Guillaume de Bure
Guillaume de Bure

Reputation: 23

Neo4j Cypher : return nodes with id as a dictionary

I have nodes with these properties:

MATCH (n:A) RETURN n
[
  {
    "name": "114s09A.1",
    "_id": "114s09A.1",
    "id": "114s09A.1",
    "created_n4j": "2020-12-21T09:56:11.256000000Z",
    "type": "A",
    "updated_n4j": "2020-12-21T09:56:11.256000000Z"
  },
  {
    "name": "114s09A.2",
    "_id": "114s09A.2",
    "id": "114s09A.2",
    "created_n4j": "2020-12-21T09:56:11.257000000Z",
    "type": "A",
    "updated_n4j": "2020-12-21T09:56:11.257000000Z"
  }
]

Is there a way to build the cypher query so that the result would be shaped as a dictionary where id would be the key ?

[
  {
    "114s09A.1": {
      "name": "114s09A.1",
      "id": "114s09A.1",
      "created_n4j": "2020-12-21T09:56:11.256000000Z",
      "type": "A",
      "updated_n4j": "2020-12-21T09:56:11.256000000Z"
    }
  },
  {
    "114s09A.2": {
      "name": "114s09A.2",
      "id": "114s09A.2",
      "created_n4j": "2020-12-21T09:56:11.257000000Z",
      "type": "A",
      "updated_n4j": "2020-12-21T09:56:11.257000000Z"
    }
  }
]

The closest I came up to so far is :

MATCH (n:A) RETURN n._id AS _id, properties(n) AS properties
[
  {
    "_id":"114s09A.1",
    "properties":{
      "name": "114s09A.1",
      "id": "114s09A.1",
      "created_n4j": "2020-12-21T09:56:11.256000000Z",
      "type": "A",
      "updated_n4j": "2020-12-21T09:56:11.256000000Z"
    }
  },
  {
    "_id":"114s09A.2",
    "properties":{
      "name": "114s09A.2",
      "id": "114s09A.2",
      "created_n4j": "2020-12-21T09:56:11.257000000Z",
      "type": "A",
      "updated_n4j": "2020-12-21T09:56:11.257000000Z"
    }
  }
]

Upvotes: 0

Views: 1239

Answers (1)

Christophe Willemsen
Christophe Willemsen

Reputation: 20185

It's not possible with the default Cypher syntax, however if you have the apoc library installed, you can do this :

MATCH (n:A)
RETURN apoc.map.setKey({}, n.id, n{.*})

Upvotes: 3

Related Questions