【问题标题】:How to bulk insert JSON array of objects in MySQL in NodeJS如何在 NodeJS 中的 MySQL 中批量插入 JSON 对象数组
【发布时间】:2021-04-30 11:19:23
【问题描述】:

我基本上是在尝试使用 NodeJS 在 MySQL 表中批量插入或更新(在重复键上)对象的 JSON 数组。有时,数组中的 JSON 对象包含不同的键值对。

一种可能的解决方案是首先遍历 JSON 数组,然后在每个对象中搜索从另一个数组(对应于表中的列)硬编码的每个键,然后将结果值推送到新的数组数组中。如果 JSON 对象中不存在键,则填充“NULL”值。

非常感谢您提前提供的任何建议!

var keys = ['customerId','subscriptionId','billingMethod','skuId','creationTime','planName','isCommitmentPlan','startTime','endTime','numberOfSeats','licensedNumberOfSeats','maximumNumberOfSeats','isInTrial','trialEndTime','renewalType','purchaseOrderId','status''customerDomain','skuName','suspensionReasons','dealCode'];

var demo_api = [{
  "kind": "reseller#subscription",
  "customerId": "C0123456",
  "subscriptionId": "123",
  "billingMethod": "ONLINE",
  "skuId": "Google-Apps-Unlimited",
  "creationTime": "1331647980142",
  "plan": {
    "planName": "ANNUAL",
    "isCommitmentPlan": true,
    "commitmentInterval": {
      "startTime": "1331647980142",
      "endTime": "1363183980142"
    }
  },
  "seats": {
    "kind": "subscriptions#seats",
    "numberOfSeats": 10,
    "licensedNumberOfSeats": 10,
    "maximumNumberOfSeats": 500
  },
  "trialSettings": {
    "isInTrial": false
  },
  "renewalSettings": {
    "kind": "subscriptions#renewalSettings",
    "renewalType": "SWITCH_TO_PAY_AS_YOU_GO"
  },
  "purchaseOrderId": "my_example.com_annual_1",
  "status": "ACTIVE",
  "customerDomain": "my_example.com",
  "skuName": "G Suite Business"
},
{
  "kind": "reseller#subscription",
  "customerId": "D0123456",
  "subscriptionId": "456",
  "billingMethod": "ONLINE",
  "skuId": "Google-Apps-For-Business",
  "creationTime": "1331647980142",
  "plan": {
    "planName": "FLEXIBLE",
    "isCommitmentPlan": false
  },
  "seats": {
    "kind": "subscriptions#seats",
    "licensedNumberOfSeats": 0,
    "maximumNumberOfSeats": 10
  },
  "trialSettings": {
    "isInTrial": false
  },
  "purchaseOrderId": "my_example_flex_1",
  "status": "ACTIVE",
  "customerDomain": "my_example2.com",
  "skuName": "G Suite Business"
}];

想要的结果:

var result = [
    ["C0123456", "123", "ONLINE", "Google-Apps-Unlimited", "1331647980142", "ANNUAL", true, "1331647980142", "1363183980142", 10, 10, 500, false, "NULL", "SWITCH_TO_PAY_AS_YOU_GO", "my_example.com_annual_1", "ACTIVE", "my_example.com", "G Suite Basic", "NULL", "NULL"],
    ["D0123456", "456", "ONLINE", "Google-Apps-For-Business", "1331647980142", "FLEXIBLE", false, "NULL", "NULL", "NULL", 0, 0, false, "NULL", "NULL", "my_example_flex_1", "ACTIVE", "my_example2.com", "G Suite Business", "NULL", "NULL"]
];

【问题讨论】:

标签: javascript node.js


【解决方案1】:

你可以试试这样的:

  var myArray = new Array();

data.forEach((player) => {
  console.log(player.id);
  console.log(player);
  var playerModel ={
    id : player.id,
    firstname : player.firstname,
    lastname : player.lastname,
    position : player.position,
    price : player.price,
    appearences : player.appearences,
    goals : player.goals,
    assists : player.assists,
    cleansheets : player.cleansheets,
    redcards : player.redcards,
    yellowcards : player.yellowcards,
    image : player.image,
    clubid : player.clubid,
  };
  console.log("model"+playerModel.position);
  myArray.push(playerModel);
});

这里你需要用你创建的模型来改变我代码中的模型播放器,并像这样替换,只需按照你的 json 端点:

 var Mymodel = {
    bind: player.bind,
    ............
    ....
    }

最后一步是 myArray 是你的结果数组,你需要循环它才能看到结果

  for(let item of myArray) {
    console.log(item);
 console.log(item.bind);
.......................
}

【讨论】:

  • 您好票价,感谢您的回复。问题是并非所有对象都包含相同的属性。对于那些不存在的属性,我想分配一个“空”值。
  • 好的,那么你需要一个条件,如果属性不存在,你这样做:object.property = NULL,然后将分配Null值
  • 条件是测试属性是否存在
猜你喜欢
  • 2020-09-02
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 2021-10-25
  • 2018-05-31
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多