Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB(Java)if语句等于value_Java_Mongodb_If Statement_Equals - Fatal编程技术网

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,它可以正常工作。