Search code examples
node.jsneo4jnode-neo4j

How to get node-neo4j return nodes with data only


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!


Solution

  • 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.