我想使用java驱动程序查询MongoDB数据库。
例如,这些是 mycoll 集合的两个文档:
{
"_id":"4d85c7039ab0fd70a117d73100",
"person_id":"100",
"name":"UFlQsAWmgp",
"surname":"ohJTiGRqsM",
"place":"UtXVBKoajo",
"age":90
}
{
"_id":"4d85c7039ab0fd70a117d73101",
"person_id":"101",
"name":"AosStuizVj",
"surname":"BCYcWKDTdu",
"place":"jRlEszdLOw",
"age":62
}
现在我想使用Java驱动程序检索所有5岁的人:
try
{
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
String database = "test";
MongoDatabase db = mongoClient.getDatabase(database);
System.out.println("*****Connect to database successfully*****");
String collection="mycoll";
MongoCollection coll = db.getCollection(collection);
System.out.println("*****Connect to collection " + "\"" + collection + "\" successfully******");
BasicDBObject query = new BasicDBObject();
query.put("age", 5);
DBCursor cursor = coll.find(query);
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
此代码不起作用。 Eclipse没有重新认识
coll.find(....)
答案 0 :(得分:3)
您的collection
变量是String
。您应该将find(...)
与coll
变量一起使用。
魔鬼显然在编译器错误的细节中 - 它表示&#34; ...未定义类型字符串 &#34; < / p>