[JAVA-543] Update of nonexistent record not throwing exception Created: 23/Mar/12  Updated: 11/Sep/19  Resolved: 23/Mar/12

Status: Closed
Project: Java Driver
Component/s: API
Affects Version/s: 2.7.2
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Jeremy Johnson Assignee: Unassigned
Resolution: Done Votes: 0
Labels: update
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

If my collection has WriteConcern.SAFE, shouldn't trying to update( upsert=FALSE) a non-existent record throw an exception? This isn't happening in my application.

Here's a contrived example that exhibits the behavior:

DBCollection collection = getDb().getCollection("empty_collection");
collection.setWriteConcern( WriteConcern.SAFE);

collection.update( new BasicDBObject().append("_id", "doesntExist"), new BasicDBObject().append("some", "value"), false, false);

No exception is thrown here. If this is by design, how should I test for attempts to update non-existent records?



 Comments   
Comment by Scott Hernandez (Inactive) [ 23/Mar/12 ]

Yes, this is by design. If you get the result from the update you can see how many were updated and apply your own logic. This is also true with remove/delete.

WriteResult wr = collection.update(...);
wr.getN() //update count

http://api.mongodb.org/java/current/com/mongodb/WriteResult.html#getN()

Generated at Thu Feb 08 08:52:32 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.