实施例: 如果我在我的程序中给出姓名,我还需要获得相应年龄的年龄{“姓名”:“msr”,“年龄”:24}喜欢这些
package mongojava;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
public class javaconn {
public static void main(String[] args) {
MongoClient client = new MongoClient("localhost",27017);
DB db = client.getDB( "admin" );
DBCollection collection = db.getCollection("admin");
DBObject dbo = collection.findOne();
//String name = (String) dbo.get("Name");
//String age = (String) dbo.get("age");
BasicDBObject query = new BasicDBObject();
query.put("Name", "Siddareddy");
DBCursor cursor = collection.find(dbo);
BasicDBObject newDocument = new BasicDBObject();
newDocument.put("age", "24");
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
}
}
答案 0 :(得分:0)
尝试使用以下查询
Document doc = collection.find(eq("Name", "Siddareddy")).first();