如何在Node.js中使用猫鼬将数组json拆分为单独的记录

时间:2018-11-11 12:58:12

标签: javascript node.js mongodb mongoose nosql

我有很多从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循环中运行并将其插入,或者有某种方法可以以更优化的方式进行操作?

1 个答案:

答案 0 :(得分:0)

db.collection.insertMany()

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