【问题标题】:How to split an array json into separate records using mongoose in Nodejs如何在 Nodejs 中使用 mongoose 将数组 json 拆分为单独的记录
【发布时间】:2019-04-14 09:29:20
【问题描述】:

我有大量从 API 获得的对象,这是它的 URL: https://api.opensea.io/api/v1/asset_contracts/ 我想把这个 JSON 传递给 MongoDB,以便为这个数组中的每个对象创建一个记录(目前有 ~230 个)。

最好的方法是什么?我目前有这样的json:

fetch("https://api.opensea.io/api/v1/asset_contrcts/"}).then(r => r.json())
.then(data => {
  // Add all of the data to a collection;
  return data;
});

}

最好的方法是什么?在每个对象上运行一个 for 循环并插入它,或者有什么方法可以以更优化的方式执行它?

【问题讨论】:

标签: javascript node.js mongodb mongoose nosql


【解决方案1】:

db.collection.insertMany()

这将解决您的问题。 您无需将数组 json 拆分为单独的记录。只需将数组传递给 inserMany() 它将隐式处理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多