Java Code Examples for com.mongodb.client.MongoCollection.findOneAndDelete()

The following are Jave code examples for showing how to use findOneAndDelete() of the com.mongodb.client.MongoCollection class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: database-transform-tool   File: MongoDBFactory.java   View Source Code Vote up 6 votes
/**
 * @decription 删除数据
 * @author yi.zhang
 * @time 2017年6月2日 下午6:19:25
 * @param table	文档名称(表名)
 * @param obj
 * @return
 */
public int delete(String table, Object obj) {
	try {
		if(session==null){
			init(servers, database, schema, username, password);
		}
		MongoCollection<Document> collection = session.getCollection(table);
		if (collection == null) {
			return 0;
		}
		JSONObject json = JSON.parseObject(JSON.toJSONString(obj));
		collection.findOneAndDelete(Filters.eq("_id", json.containsKey("_id")?json.get("_id"):json.get("id")));
		return 1;
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return -1;
}