繁体   English   中英

如何在NodeJS中的MySQL中批量插入JSON对象数组

[英]How to bulk insert JSON array of objects in MySQL in NodeJS

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

一种可能的解决方案可能是首先遍历 JSON 数组,然后在每个 object 中搜索从另一个数组硬编码的每个键(对应于表中的列),然后将结果值推送到 ZA3CBC3F9D0CE2F2C1754E1B671CZD 的新数组中。 如果 JSON object 中不存在密钥,则填写“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"]
];

你可以尝试这样的事情:

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

在这里,您需要将我的代码中的 model 播放器更改为您创建的 model,然后像这样替换,只需按照您的 json 端点即可:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM