Reputation: 10210
How can I check if document write is completed or not as mongodb is based on the philosophy of 'fire and forget'?
Can I write a program like this:
db.addresses.insert(....)
db.addresses.find(filter...
Second statement find is based on the inserted document in the first step.
How can we check that the document is written before doing some/any operation on it?
Upvotes: 0
Views: 320
Reputation: 230306
You should write in "safe mode" then.
When safe mode is activated, the driver will perform write operation and then ask for confirmation. This essentially doubles your db request throughput and increases processing time for your web requests, but you can be sure that write completed successfully.
I've found this example of using safe mode in PHP driver (can't tell if it's good or not. I code in Ruby, so all PHP looks broken to me :) )
$insert_options = array("safe" => 2, "fsync" => TRUE);
$email_array = array( "_id" => new MongoId($index),
"email" => strtolower(trim($email)),
"verified" => 0,
"date" => date("Y-m-d G:i:s",time())
$this->mongo_db->email->insert($email,$insert_options);
Upvotes: 1