Viraj Dhamal
Viraj Dhamal

Reputation: 5325

How to handle exception in MongoDB?

I am using mongodb database. What the MongoDB will return if there any exception while saving,updating or deleting data(or object)?

How to handle exception in MongoDB?

Upvotes: 0

Views: 232

Answers (2)

Sushant Gupta
Sushant Gupta

Reputation: 9468

While making a connection to mongod, set safe to true, for instance here is how it can be done using python driver (pymongo)

from pymongo import Connection
connection = Connection('localhost', 27017, safe = True)

By doing so you will get write acknowledgements, else it would be simple fire and forget.

Upvotes: 2

Sammaye
Sammaye

Reputation: 43884

MongoDB by default (in some drivers) does not enforce a safe mode whereby the database will physically respond to every call you make to say whether or not it was successful.

However in most drivers there is a getLastError() and you can, of course, enforce safe mode on calls using something similar to:

update({},{},{safe:true});

Using both of these methods will allow you to return problems MongoDB might get in handling your operations.

Upvotes: 3

Related Questions