MongoDB(Java)if语句等于value
因此,我正在使用MongoDB(Java)if语句等于value,java,mongodb,if-statement,equals,Java,Mongodb,If Statement,Equals,因此,我正在使用if语句编写一段代码,在if语句中,我想检查MongoDB的值是否等于“test” 我现在有这个,它不是很多,但我真的不知道如何做到这一点 if (DBManager.getInstance().getTestCollection()) { 我的MongoDB示例 { "_id": { "$oid": "56fd47ea18de75129095d841" }, "foo": "test" } 一旦你得到了这个集合,你就需要在它上面找到光标并在它之间循环。见下文
if
语句编写一段代码,在if
语句中,我想检查MongoDB的值是否等于“test”
我现在有这个,它不是很多,但我真的不知道如何做到这一点
if (DBManager.getInstance().getTestCollection()) {
我的MongoDB示例
{
"_id": {
"$oid": "56fd47ea18de75129095d841"
},
"foo": "test"
}
一旦你得到了这个集合,你就需要在它上面找到光标并在它之间循环。见下文
DBCollection testCollection = DBManager.getInstance().getTestCollection()
DBCursor cursor = testCollection.find();
while (cursor.hasNext()) {
BasicDBObject obj = (BasicDBObject) cursor.next();
if ("test".equals(obj.get("foo")) {
System.out.println("Found");
}
}
getTestCollection()返回什么?
public MongoCollection getTestCollection(){return database.getCollection(“test_collection”)}
my MongoDB的值是什么意思?显示数据和要查找的字段/项目的示例。(用相关信息编辑您的问题)DBCollection testCollection=DBManager.getInstance().getTestCollection()
想让我投com.mongodb.DBCollection代码>是否尝试强制转换?是的,DBCollection testCollection=(DBCollection)DBManager.getInstance().getTestCollection()
但是现在我得到了这个错误,原因是:java.lang.ClassCastException:com.mongodb.MongoCollectionImpl不能强制转换为com.mongodb.DBCollection
您使用的是哪个版本的mongo驱动程序?我使用的是版本3,它可以正常工作。