0

I have a large array of object that I get from an API, this is its URL: https://api.opensea.io/api/v1/asset_contracts/ I would like to take this JSON and pass it to MongoDB in a way that will create a record for each object in this array (currently there are ~230).

What will be the best way to do it? I currently have the json like so:

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

}

What is the best way to do so? Run in a for loop over each of the objects and insert it or is there some way to do it in a more optimal way?

Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59
Randomizer
  • 475
  • 7
  • 19
  • Use [insertMany](https://docs.mongodb.com/manual/reference/method/db.collection.insertMany/) operation – Ashh Nov 11 '18 at 13:02
  • Possible duplicate of https://stackoverflow.com/questions/37379180/bulk-insert-in-mongodb-using-mongoose/ – chridam Nov 11 '18 at 13:06

1 Answers1

0

db.collection.insertMany()

this will solve your problem. you need not split the array json into separate records. just pass the array to inserMany() it will handled implicitly.

Abdus Sattar
  • 239
  • 1
  • 4
  • 13