使用Mongodb和Java检查文档是否存在?
Check whether the document exists using Mongodb and Java?
我创建了一个简单的 java 应用程序作为我的大学迷你项目,其中一个模块允许用户执行插入、删除、更新和搜索等操作。
出于验证目的,如果用户尝试删除数据库中不存在的记录,我希望向用户显示一条错误消息,例如
“抱歉,找不到记录”。
我已经尝试 try
catch
块来检查 mongodb 是否在未找到文档时抛出异常,但没有成功。我是 Java 和 Mongodb 的新人,需要帮助。
这是我的 deleteActionPerformed 代码和我尝试过的代码:
private void deleteActionPerformed(java.awt.event.ActionEvent evt) {
try {
// my collection name is activity
DBCollection col = db.getCollection("activity");
// Tid is the TextField in which i am taking input of _id
if(!Tid.getText().equals("")) {
col.remove(new BasicDBObject().append("_id",(Object)Tid.getText()));
} else {
JOptionPane.showMessageDialog(null,"Please Enter the ID");
}
} catch(Exception e){
JOptionPane.showMessageDialog(null,"Record not Found " + e);
}
}
try
catch
块没有生成未找到的类型异常。
这可能不是最有效的方法,但它应该有效。
我从我寻找特定文档值(_id
除外)的一些代码改编而来。
_id
可能有专门的方法。
/**
* Checks if an activity exists with a given id. if no such activity exists
* returns false. Returns true for one or more activities with a matching id.
*
* @param db
* @param id
* @return boolean - true if one or more functions with matching names exit.
*/
public static boolean activityExists(MongoDatabase db, ObjectId id) {
FindIterable<Document> iterable = db.getCollection("activity")
.find(new Document("_id", id));
return iterable.first() != null;
}
编辑: 似乎最好使用计数方法。请参考以下回答:
在您的情况下,使用 find() + limit() 的速度要快得多,因为 findOne() 将始终读取 + return 文档(如果存在)。 find() 只是 return 一个游标(或不是),并且只有在遍历游标时才读取数据。
所以代替:
db.collection.findOne({_id: “myId”}, {_id: 1})
你应该使用:
db.collection.find({_id: “myId”}, {_id: 1}).limit(1)
我创建了一个简单的 java 应用程序作为我的大学迷你项目,其中一个模块允许用户执行插入、删除、更新和搜索等操作。
出于验证目的,如果用户尝试删除数据库中不存在的记录,我希望向用户显示一条错误消息,例如 “抱歉,找不到记录”。
我已经尝试 try
catch
块来检查 mongodb 是否在未找到文档时抛出异常,但没有成功。我是 Java 和 Mongodb 的新人,需要帮助。
这是我的 deleteActionPerformed 代码和我尝试过的代码:
private void deleteActionPerformed(java.awt.event.ActionEvent evt) {
try {
// my collection name is activity
DBCollection col = db.getCollection("activity");
// Tid is the TextField in which i am taking input of _id
if(!Tid.getText().equals("")) {
col.remove(new BasicDBObject().append("_id",(Object)Tid.getText()));
} else {
JOptionPane.showMessageDialog(null,"Please Enter the ID");
}
} catch(Exception e){
JOptionPane.showMessageDialog(null,"Record not Found " + e);
}
}
try
catch
块没有生成未找到的类型异常。
这可能不是最有效的方法,但它应该有效。
我从我寻找特定文档值(_id
除外)的一些代码改编而来。
_id
可能有专门的方法。
/**
* Checks if an activity exists with a given id. if no such activity exists
* returns false. Returns true for one or more activities with a matching id.
*
* @param db
* @param id
* @return boolean - true if one or more functions with matching names exit.
*/
public static boolean activityExists(MongoDatabase db, ObjectId id) {
FindIterable<Document> iterable = db.getCollection("activity")
.find(new Document("_id", id));
return iterable.first() != null;
}
编辑: 似乎最好使用计数方法。请参考以下回答:
在您的情况下,使用 find() + limit() 的速度要快得多,因为 findOne() 将始终读取 + return 文档(如果存在)。 find() 只是 return 一个游标(或不是),并且只有在遍历游标时才读取数据。
所以代替:
db.collection.findOne({_id: “myId”}, {_id: 1})
你应该使用:
db.collection.find({_id: “myId”}, {_id: 1}).limit(1)