MongoDB:使用JAVA将字符串字段转换为Int

MongoDB:使用JAVA将字符串字段转换为Int,java,mongodb,mongodb-query,Java,Mongodb,Mongodb Query,我正在学习mongoDB。我有一个像这样的收藏: { "_id" : ObjectId("558cf353209c021b5a2fcbe5"), "term" : "gamma red eye tennis dampener", "year" : "2015", "month" : "05", "day" : "29", "hour" : "09", "dayofyear" : "176", "weekofyear" : "26",

我正在学习mongoDB。我有一个像这样的收藏:

{
    "_id" : ObjectId("558cf353209c021b5a2fcbe5"),
    "term" : "gamma red eye tennis dampener",
    "year" : "2015",
    "month" : "05",
    "day" : "29",
    "hour" : "09",
    "dayofyear" : "176",
    "weekofyear" : "26",
    "productcount" : "1",
    "count" : "1"
}
{
    "_id" : ObjectId("578cf353209c021b5a2fcbe5"),
    "term" : "tennis dampener",
    "year" : "2015",
    "month" : "05",
    "day" : "29",
    "hour" : "09",
    "dayofyear" : "176",
    "weekofyear" : "26",
    "productcount" : "1",
    "count" : "7"
}
{
    "_id" : ObjectId("568cf353209c021b5a2fcbe5"),
    "term" : "gamma ",
    "year" : "2015",
    "month" : "05",
    "day" : "29",
    "hour" : "09",
    "dayofyear" : "176",
    "weekofyear" : "26",
    "productcount" : "1",
    "count" : "4"
}
这里的字段count是字符串。我希望遍历所有文档,并使用JAVA将其转换为INT

我可以通过控制台使用以下命令执行此操作:

db.tq.find().forEach(function(obj) {
    obj.count= NumberInt(obj.count);
    db.tq.save(obj);
})

如何在java中实现它?

您应该这样做-

DBCollection collection = db.getCollection("collection");
BasicDBObject document = new BasicDBObject();
DBCursor cursor = collection.find();
while (cursor.hasNext()) {
    DBObject cur = cursor.next();
    String id = cur.get("_id").toString();
    String c = cur.get("count").toString();
    int updateCount = Integer.parseInt(c);  //change to int
    BasicDBObject updateQuery = new BasicDBObject();
    updateQuery.append("$set", new BasicDBObject().append("count", updateCount));
    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("_id", new ObjectId(id));
    collection.update(searchQuery, updateQuery);
}
整数.parseInt(字符串)