如何使用Nodejs中的mongose将数组json拆分为单独的记录



我有一个从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循环并插入它,或者有没有更优化的方法?

db.collection.insertMany((

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

最新更新