0

I'm using collection.insert then in the callback collection.findAndModify

About 10% of the time collection.findAndModify fails to find the document I just inserted, even though it's being executed after the insert callback. Why is this possible? How do I deal with that?

enter image description here

I insert it, then try to modify it, but it's not there, then try to modify it again and it's there.

1 Answer 1

1

You should give the second command in the callback as the insert is asynchronous. If you are using the mongodb native driver for node,

collection.insert(document, function(err, records){
     //call collection.findAndModify here

});

Check the docs

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I am using npm mongodb, and this is exactly what I'm doing, I'm using collection.insert then in the callback collection.findAndModify I guess I'll need to create a simple way to replicate the issue
@StephenSarcsamKamenar Sorry I overlooked that part in your question. All hell will break loose if this is true, something must be wrong.
Yes, please add as simple a code snippet as you can find to recreate the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.