Thursday, 20 April 2017

Why mongodb's find() method is not able to find the recently inserted document in Nodejs

blabla.find({ 'url': obj.urlObj.url }).toArray((err, results) => {
        if (err) { return err }

        if (results.length === 0) {
          blabla.insert({
            url: obj.urlObj.url,
            views: 1
          })
        } else {
          blabla.update({
            _id: results[0]._id
          },
            {
              $inc: { views: 1 }
            })
        }
      })

This piece of code is under a loop where I get obj, now I'm trying to find a url in blabla collection and if that url is not present then I am inserting that url. If it is present then I am just incrementing the number of views by 1.

It is suppose to give me only unique set of urls in the collection but I see some urls to be duplicated. What can be the reason of this duplication, is the insert task is not able to finish and it tries to find the same url and couldn't find?



via Actung

No comments:

Post a Comment