Search code examples
javascriptnode.jsmongodbmongoose-schema

saving documents to mongoDB preventing duplicates


I'm trying to save multiple documents in mongodb using mongoose; and I'm also willing to prevent duplicates. my function looks sth like this:

const Stock = require('./models/stock')
let _symbol = 'symb'

const writeToDB = async (dataObj) => {
    try {

        let stock = await Stock.find({symbol : _symbol } , function (err) {
            if(err) return null
        })

    if (!stock) {
        stock = new Stock({
            dataObj
        })
        await stock.save()
        console.log(`${symbol} is successfully saved to database`)
    } else {
        stock = await Stock.updateMany(
            dataObj, function (err) {
            if (err) {
                console.log(err)
            } else {
                console.log(`${symbol} successfully added`)
            }
        })
    }
    } catch (error) {
        console.log(error)
    }
    
}

but I keep getting timeout error. can someone pls inform me what's wrong.

update

with a well handled connection approach findOneAndUpdate()works fine


Solution

  • Using the upsert option, in findOneAndUpdate(). An upsert behaves like a normal findOneAndUpdate() if it finds a document that matches filter. But, if no document matches filter, MongoDB will insert one by combining filter and update as shown below

    var query = {symbol : _symbol };
    
    try{
       let result = await Stock.findOneAndUpdate(query, dataObj, {upsert: true})
    }
    catch(err){
      console.log();
    }
    
    

    if you have a big collection, for increase speed findOneAndUpdate(), you should indexed symbol field.

    when you use async await, it's better don't use callback and use try catch