Mongodb 将 http.post 中的 json 插入到集合中会更改 json
Mongodb inserting json from http.post into collection changes the json
我正在使用 mongodb、nodejs 等为我的移动应用程序制作管理页面。我正在尝试将现有 json 文件插入 mongodb 集合。我尝试了 mongoimport 但它没有用(我通读并尝试了几个关于它的 Whosebug 讨论)。所以我发现我可以通过 http POST 发送它并使用 insert().
我有三个本地主机服务器 运行:mongodb、应用程序服务器和网站管理页面
json 文件看起来像:
{"data_version":1,"drivers_teams":{"drivers":{"4":{"name":"Hofer","firstname": ... }}}
客户POST
$.getJSON("initialData.json", function(json) {
$.ajax({
url: 'localhost:3080/client_data',
type: 'POST',
data: json,
success: function(res) {
console.log(res);
}.bind(this),
error: function(xhr, status, err) {
console.error(url, status, err.toString());
}.bind(this)
});
}.bind(this));
服务器POST
app.use(bodyParser.json({limit: '1000kb', parameterLimit: 5000}));
app.use(bodyParser.urlencoded({limit: '1000kb', parameterLimit: 5000, extended: false}));
app.post('/client_data/', (req, res) => {
const db = req.db;
const collection = db.get('collection');
collection.insert(req.body, function (err, doc) {
if (err) {
res.status(500).send('Invalid request');
return;
}
res.send('ok');
});
});
到目前为止一切顺利,但在我设法插入数据并询问服务器你得到了什么之后?我得到:
{"_id":"57872f9fe51246dc1b3bbc93","data_version":"1","drivers_teams[drivers][4][name]":"Hofer","drivers_teams[drivers][4][firstname] ... }}}
我不能再使用这些数据了。所以问题:当它来自 http 请求正文时,如何按原样插入嵌套 json?
编辑
好的,知道如何执行 AS-IS insert()。我将 json 直接加载到集合中,而不是 http.post;
服务器
import mydata from './initialData.json';
...
collection.insert(mydata, function (err, doc) { ... });
仍然对我在请求正文中做错了什么感到困惑。因为现在如果我做 collection.update();来自客户端的更新元素显然被括号符号弄乱了。
解决方案是更改 url 编码的 bodyParser 选项
app.use(bodyParser.urlencoded({ extended: true }));
这真的是违反直觉的,至少对我来说是这样,因为我正在寻找与 json 有关的问题,而不是表格...
唯一的缺点是一切都是字符串而不是它们的真实类型,但是一旦我走到这一步我就不在乎了。
编辑
为了在服务器端获得正确的数据类型,我需要 JSON.stringify(data) 并在 POST options
中添加 contentType: 'application/json'
post: function(data) {
$.ajax({
url: url,
type: 'POST',
data: JSON.stringify(data),
contentType: 'application/json',
success: function(res) {
console.log(res)
}.bind(this),
error: function(xhr, status, err) {
console.error(url, status, err.toString());
}.bind(this)
});
},
我正在使用 mongodb、nodejs 等为我的移动应用程序制作管理页面。我正在尝试将现有 json 文件插入 mongodb 集合。我尝试了 mongoimport 但它没有用(我通读并尝试了几个关于它的 Whosebug 讨论)。所以我发现我可以通过 http POST 发送它并使用 insert().
我有三个本地主机服务器 运行:mongodb、应用程序服务器和网站管理页面
json 文件看起来像:
{"data_version":1,"drivers_teams":{"drivers":{"4":{"name":"Hofer","firstname": ... }}}
客户POST
$.getJSON("initialData.json", function(json) {
$.ajax({
url: 'localhost:3080/client_data',
type: 'POST',
data: json,
success: function(res) {
console.log(res);
}.bind(this),
error: function(xhr, status, err) {
console.error(url, status, err.toString());
}.bind(this)
});
}.bind(this));
服务器POST
app.use(bodyParser.json({limit: '1000kb', parameterLimit: 5000}));
app.use(bodyParser.urlencoded({limit: '1000kb', parameterLimit: 5000, extended: false}));
app.post('/client_data/', (req, res) => {
const db = req.db;
const collection = db.get('collection');
collection.insert(req.body, function (err, doc) {
if (err) {
res.status(500).send('Invalid request');
return;
}
res.send('ok');
});
});
到目前为止一切顺利,但在我设法插入数据并询问服务器你得到了什么之后?我得到:
{"_id":"57872f9fe51246dc1b3bbc93","data_version":"1","drivers_teams[drivers][4][name]":"Hofer","drivers_teams[drivers][4][firstname] ... }}}
我不能再使用这些数据了。所以问题:当它来自 http 请求正文时,如何按原样插入嵌套 json?
编辑
好的,知道如何执行 AS-IS insert()。我将 json 直接加载到集合中,而不是 http.post;
服务器
import mydata from './initialData.json';
...
collection.insert(mydata, function (err, doc) { ... });
仍然对我在请求正文中做错了什么感到困惑。因为现在如果我做 collection.update();来自客户端的更新元素显然被括号符号弄乱了。
解决方案是更改 url 编码的 bodyParser 选项
app.use(bodyParser.urlencoded({ extended: true }));
这真的是违反直觉的,至少对我来说是这样,因为我正在寻找与 json 有关的问题,而不是表格... 唯一的缺点是一切都是字符串而不是它们的真实类型,但是一旦我走到这一步我就不在乎了。
编辑
为了在服务器端获得正确的数据类型,我需要 JSON.stringify(data) 并在 POST options
中添加 contentType: 'application/json' post: function(data) {
$.ajax({
url: url,
type: 'POST',
data: JSON.stringify(data),
contentType: 'application/json',
success: function(res) {
console.log(res)
}.bind(this),
error: function(xhr, status, err) {
console.error(url, status, err.toString());
}.bind(this)
});
},