1

我正在使用 mongo jackson mapper 通过其 id 删除一个对象:

WriteResult<Restaurant, String> writeResult = restaurantCollection.removeById(restaurantIdToDelete);
String restaurantId = writeResult.getSavedId(); 

我得到

com.mongodb.MongoException: No objects to return

由于该对象不再存在,系统无法检索其 id 和/或 getSavedId() 方法仅用于保存操作。

我怎么知道删除操作是否顺利?

4

1 回答 1

0

I found this workaround:

public String removeRestaurant(String restaurantIdToDelete){

    String restaurantId = null;

    WriteResult<Restaurant, String> writeResult = restaurantCollection.removeById(restaurantIdToDelete);

    if(writeResult != null)
        restaurantId = restaurantIdToDelete; 

    return restaurantId;

}

So in theory if something goes wrong either WriteResult should be null or a MongoException would have been thrown... not sure 100% though.

于 2012-11-05T06:50:32.197 回答