Arto Kalishian
Arto Kalishian

Reputation: 546

Mongoose await doc.save() Incomplete

I fetch a JSON object from a remote server successfully. The JSON file is an array of objects. I loop over them and during the loop I try to save each document object with mongoose as below. But only 50 records are saved out of 500. So obviously I am doing something wrong with async/await. Your help is appreciated.

Edit: Added more of the code upon request from users.

const mongoose = require('mongoose')
const fetch = require('fetch-json')
const PriceModel = mongoose.model('Price', priceSchema);

getPrices = async () => {
  try {
    const url = 'https://api.binance.com/api/v3/ticker/price'
    const params = ''
    const jsonData = await fetch.get(url, params)
    return jsonData

  } catch (e) {
    console.log('Error caught during getPrices: ', e.message)
  }
}

mongoose.connect('mongodb://localhost/prices', { useNewUrlParser: true });

const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
  console.log('we\'re connected')
});

const priceSchema = new mongoose.Schema({
  timestamp: Date,
  symbol: String,
  price: Number
});

    persistData = async () => {

      try {
        const timestamp = Date.now()
        const allPrices = await getPrices()

        for (const element of allPrices) {
          let doc = new PriceModel()
          doc.timestamp = timestamp
          doc.symbol = element.symbol
          doc.price = element.price
          await doc.save()
        }
      } catch (e) {
        console.log('Error caught during db save: ', e.message)
      }
    }

Upvotes: 1

Views: 543

Answers (1)

Alexandru Olaru
Alexandru Olaru

Reputation: 7092

As from version 4.4 mongoose does support insertMany operation. So a much faster algorithm will be to prepare your needed data:

const priceModels = [];

for (const element of allPrices) {
  let doc = {
     timestamp,
     ...element,
  }
  priceModels.push(doc)
}

And after you prepared all the objects, you make one insert to the database:

await PriceModel.insertMany(priceModels);

Upvotes: 4

Related Questions