4

我不明白如何从 orientjs 查询中获取标准 JSON。我看到人们谈论“序列化”结果,但我不明白为什么或如何做到这一点。有一种toJSON()方法,但我只看到它与 fetchplans 等一起使用......

我正在尝试将流传输到 csv 文件,但由于 JSON 格式不正确,它无法正常工作。

我很想解释如何以及何时进行序列化。:-)

我的查询:

  return db.query(
    `SELECT 
      id,
      name,
      out('posted_to').name as page,
      out('posted_to').id as page_id,
      out('posted_to').out('is_language').name as language,
      out('posted_to').out('is_network').name as network 

    FROM post

    WHERE posted_at
      BETWEEN
        '${since}'
      AND
        '${until}'

      UNWIND 
        page,
        page_id,
        language,
        network
    `

我的结果:

[ { '@type': 'd',
    id: '207109605968597_1053732754639607',
    name: '10 maneiras pelas quais você está ferindo seus relacionamentos',
    page: 'Eu Amo o Meu Irmão',
    page_id: '207109605968597',
    language: 'portuguese',
    network: 'facebook',
    '@rid': { [String: '#-2:1'] cluster: -2, position: 1 },
    '@version': 0 },
  { '@type': 'd',
    id: '268487636604575_822548567865143',
    name: '10 maneiras pelas quais você está ferindo seus relacionamentos',
    page: 'Amo meus Filhos',
    page_id: '268487636604575',
    language: 'portuguese',
    network: 'facebook',
    '@rid': { [String: '#-2:3'] cluster: -2, position: 3 },
    '@version': 0 }]
4

2 回答 2

0

我找到了一种对我有用的方法。而不是使用:

db.query()

我在节点中使用http请求来查询数据库。在OrientDB 文档上还说你只得到 JSON 格式的结果。这样,如果您在数据库中查询,您将始终获得有效的 JSON。

为了发出 http 请求,我使用了请求模块。这是一个对我有用的样本:

var request =  require("request");
var auth = "Basic " + new Buffer("root" + ":" + "root").toString("base64")

request(
        {
            url : encodeURI('http://localhost:2480/query/tech_graph/sql/'+queryInput+'/20'),
            headers : {
                "Authorization" : auth
            }
        },
        function (error, response, body) {
            console.log(body);
            return body;
        }
    );
于 2017-01-21T14:48:06.817 回答
0

这是我的数据集:

在此处输入图像描述

询问:

db.select('id','code').from('tablename').where({deleted:true}).all()
    .then(function (vertex) {
        console.log('Vertexes found: ');
        console.log(vertex);
});

输出:

Vertexes found:
[ { '@type': 'd',
    id: '6256650b-f5f2-4b55-ab79-489e8069b474',
    code: '4b7d99fa-16ed-4fdb-9baf-b33771c37cf4',
    '@rid': { [String: '#-2:0'] cluster: -2, position: 0 },
    '@version': 0 },
  { '@type': 'd',
    id: '2751c2a0-6b95-44c8-966a-4af7e240752b',
    code: '50356d95-7fe7-41b6-b7d9-53abb8ad3e6d',
    '@rid': { [String: '#-2:1'] cluster: -2, position: 1 },
    '@version': 0 } ]

如果我添加说明JSON.stringify()

询问:

db.select('id','code').from('tablename').where({deleted:true}).all()
    .then(function (vertex) {
        console.log('Vertexes found: ');
        console.log(JSON.stringify(vertex));
});

输出:

Vertexes found:
[{"@type":"d","id":"6256650b-f5f2-4b55-ab79-489e8069b474","code":"4b7d99fa-16ed-
4fdb-9baf-b33771c37cf4","@rid":"#-2:0","@version":0},{"@type":"d","id":"2751c2a0
-6b95-44c8-966a-4af7e240752b","code":"50356d95-7fe7-41b6-b7d9-53abb8ad3e6d","@ri
d":"#-2:1","@version":0}]

希望能帮助到你

于 2016-05-06T12:04:46.607 回答