0

Hi I am trying to build an application which upserts data and fetches from the mongodb baser on the userid.This approach works fine for a single user.But when i try hitting for multiple users say 25 the data fetched seems to be null. Below is my upsert code

collection.update({'USER_ID': passVal.ID},
                    {'RESPONSE': Data}, { upsert: true }, function (err) {

                        if (err) {
                            console.log("Error in saving  data");

                        }
 var query = collection.findOne({'USER_ID': passVal.ID});

        query.select('RESPONSE');
        query.exec(function (err, data) {
            if (err) return handleError(err);
            console.log(data.RESPONSE);
});

})

I always get an error insome cases as data is null.I have written the read code in the call back of upsert only.I am stuck here any help regarding this will be much helpful.

Amanda G
  • 1,931
  • 10
  • 33
  • 43
  • can you clarify what you mean by "hitting for multiple users"? – Asya Kamsky Jun 05 '13 at 03:55
  • @AsyaKamsky:I meant when i concurrently try to upsert data based on userid in nodejs server i am not able to fetch the data. – Amanda G Jun 05 '13 at 04:15
  • if your intent is to always fetch the exact document you updated you should just use findAndModify instead of separate update and find. – Asya Kamsky Jun 05 '13 at 04:39
  • @Asya Kamsky:Initially i need to insert the data and then update on further calls.Will find and modify do the same operation as upsert. – Amanda G Jun 05 '13 at 04:41
  • yes, it has the same options as update as far as that goes. – Asya Kamsky Jun 05 '13 at 04:53
  • @Asya Kamsky : what is the difference between my earlier approach and the new one.Will this solve my issue of write and read in mongodb – Amanda G Jun 05 '13 at 06:56
  • see here: http://stackoverflow.com/questions/10778493/whats-the-diff-between-findandmodify-and-update-in-mongodb/10778994#10778994 – Asya Kamsky Jun 05 '13 at 12:51

0 Answers0