How to get node-neo4j return nodes with data only

2.4k Views Asked by At

I am using node-neo4j for connecting to neo4j graph database. I noticed that whenever I tried to get all nodes (for e.g. users), the json result return has way too much information that I do not need.

Here's the code to return all users node:

User.getAll = function (callback) {
    var query = [
       'MATCH (user:User)',
       'RETURN user',
    ].join('\n');

    db.query(query, null, function (err, results) {
        if (err) return callback(err);
        var users = results.map(function (result) {
           return new User(result['user']);
        });
        callback(null, users);
    });
 };

And it gave me these json respsonse;

[
{
    "_node": {
        "_nodeNeo4j": {
            "version": "1.1.0",
            "constructor": "Node"
        },
        "_data": {
            "extensions": {},
            "outgoing_relationships": "http://localhost:7474/db/data/node/13/relationships/out",
            "labels": "http://localhost:7474/db/data/node/13/labels",
            "all_typed_relationships": "http://localhost:7474/db/data/node/13/relationships/all/{-list|&|types}",
            "traverse": "http://localhost:7474/db/data/node/13/traverse/{returnType}",
            "self": "http://localhost:7474/db/data/node/13",
            "property": "http://localhost:7474/db/data/node/13/properties/{key}",
            "properties": "http://localhost:7474/db/data/node/13/properties",
            "outgoing_typed_relationships": "http://localhost:7474/db/data/node/13/relationships/out/{-list|&|types}",
            "incoming_relationships": "http://localhost:7474/db/data/node/13/relationships/in",
            "create_relationship": "http://localhost:7474/db/data/node/13/relationships",
            "paged_traverse": "http://localhost:7474/db/data/node/13/paged/traverse/{returnType}{?pageSize,leaseTime}",
            "all_relationships": "http://localhost:7474/db/data/node/13/relationships/all",
            "incoming_typed_relationships": "http://localhost:7474/db/data/node/13/relationships/in/{-list|&|types}",
            "data": {
                "uid": "53c7a820-f0b4-11e3-af63-28373723792e",
                "name": "user1"
            }
        }
    }
},

Is there anyway to just return the "data" part of the result from the cypher? or should I strip the unwanted parts in node.js server before returning the result to the client?

Thanks!

3

There are 3 best solutions below

2
On BEST ANSWER

I managed to return just the data fields by changing the response returned as following

User.getAll = function (callback) {
var query = [
    'MATCH (user:User)',
    'RETURN user',
].join('\n');

db.query(query, null, function (err, results) {
       if (err) return callback(err);
       var users = results.map(function (result) {
          return new User(result['user']['data']);
       });
       callback(null, users);
    });
};

I changed the return new User(result['user']['data']) and it successfully returns the "data" part of the response.

2
On

you could update your query to return the fields instead of the nodes...

...
RETURN user.uid, user.name

That would make your result set more of a data 'grid' of fields - but it sounds like that's what you're looking for

0
On

I myself was struggling with this last week. Long story short, you can set a variable to a specific part of the JSON thingy like so:

var anything =JSON.stringify(results[0]._node._data.data); That line returned {\"uid\":\"53c7a820-f0b4-11e3-af63-28373723792e\",\"name\":\"user1\"} for me. Check out my example here, Trying to interpret the Node-Neo4j API. Just a btw, I had to add a closing ] to the JSON to get it to work.