簡體   English   中英

將JSON文件導入Postgres

[英]Importing JSON file to Postgres

使用Node.js,我能夠將JSON文件導入到MongoDB,但是在嘗試使用Postgres時無法進行導入。

以下是我的Node.js文件:

var fs = require('fs');
var pg = require('pg');

var json = fs.readFileSync('testfile.json').toString();

pg.connect('postgres://test:test@localhost/tl', function(err, 
client, done) {
if (err) {
    console.log('error: ', err);
    process.exit(1);
}
client.query(json, function(err, result) {
    done();
    if (err) {
        console.log('error: ', err);
        process.exit(1);
    }
    process.exit(0);
 });
});

這是我的JSON文件:

[{
"id": 43947231,
"dateModified": null,
"jobTaskId": {
    "id": 16089453
},
"billable": true,
}]

似乎連接正常,但似乎不喜歡我的JSON格式。 我嘗試了各種格式,但是存在相同的錯誤。 這是我從Postgres得到的錯誤:

ERROR:  syntax error at or near "[" at character 1
STATEMENT:

Postgresql不像MongoDB那樣使用JSON進行查詢,它使用查詢語言。 您不能只插入MongoDB命令並獲得相同的結果。 如果要使用Postgresql,則必須學習SQL。

請參閱npm上pg文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM