如何使用 Mongo Java Driver 将从 MongoDB 查询的值分配给 Java 中的变量?
int usr1count;
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase mongoDatabase = mongoClient.getDatabase("testdb3");
MongoCollection mongoCollection = mongoDatabase.getCollection("sample");
mongoCollection.drop();
mongoCollection.insertOne(new Document("_id","usr1").append("count", 1));
mongoCollection.insertOne(new Document("_id","usr2").append("count", 1));
mongoCollection.insertOne(new Document("_id","usr3").append("count", 1));
BasicDBObject query = new BasicDBObject("_id", "usr1");
FindIterable<Document> fi=mongoCollection.find();
fi.forEach(new Block<Document>() {
@Override
public void apply(final Document document) {
Integer.parseInt(document.getString("count").toString());
}
});
从数据库查询后,我必须为 usr1count(variable) 分配一个值。如何分配存在于 Integer.parseInt(document.getString("count").toString()); 中的值