【问题标题】:How to get node-neo4j return nodes with data only如何获取仅包含数据的 node-neo4j 返回节点
【发布时间】:2014-06-18 09:10:08
【问题描述】:

我正在使用 node-neo4j 连接到 neo4j 图形数据库。 我注意到,每当我尝试获取所有节点(例如用户)时,json 结果返回包含太多我不需要的信息。

这是返回所有用户节点的代码:

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);
    });
 };

它给了我这些 json 响应;

[
{
    "_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"
            }
        }
    }
},

有没有办法只从密码返回结果的“数据”部分?或者我应该在将结果返回给客户端之前去除 node.js 服务器中不需要的部分吗?

谢谢!

【问题讨论】:

    标签: node.js neo4j node-neo4j


    【解决方案1】:

    通过更改返回的响应,我设法只返回数据字段

    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);
        });
    };
    

    我更改了 return new User(result['user']['data']) 并成功返回响应的“数据”部分。

    【讨论】:

    • 有更好的方法吗?
    • 更好的方法是使用仅返回节点属性的新事务密码端点:docs.neo4j.org/chunked/milestone/rest-api-transactional.html
    【解决方案2】:

    您可以更新查询以返回字段而不是节点...

    ...
    RETURN user.uid, user.name
    

    这将使您的结果集更像是字段的数据“网格” - 但听起来这就是您要寻找的内容

    【讨论】:

    • 这行得通,但是如果我想返回该节点上的所有属性/字段怎么办?例如,我有 10 个字段,我想返回所有字段。谢谢
    • 目前似乎没有返回节点属性名称列表(或包含值的映射)的功能。有趣的是,如果您使用直接的 Rest API:docs.neo4j.org/chunked/stable/…,您将获得该特定节点的属性列表。
    【解决方案3】:

    上周我自己也在为此苦苦挣扎。长话短说,您可以将变量设置为 JSON 的特定部分,如下所示:

    var anything =JSON.stringify(results[0]._node._data.data); 那条线为我返回了{\"uid\":\"53c7a820-f0b4-11e3-af63-28373723792e\",\"name\":\"user1\"}。 在这里查看我的示例,Trying to interpret the Node-Neo4j API。顺便说一句,我必须在 JSON 中添加一个结束 ] 才能让它工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-30
      • 1970-01-01
      相关资源
      最近更新 更多