我有一個蒙戈DB 5萬個條目,看起來像這樣:MapReduce的MongoDB的用戶代理
{
"_id" : ObjectId("525facace4b0c1f5e78753ea"),
"productId" : null,
"name" : "example name",
"time" : ISODate("2013-10-17T09:23:56.131Z"),
"type" : "hover",
"url" : "www.example.com",
"userAgent" : "curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 openssl/0.9.8r zlib/1.2.5"
}
我需要添加到每個條目一個新的領域被稱爲device
這將有兩種價值desktop
或mobile
。這意味着,我們的目標將是有以下種類的條目:
{
"_id" : ObjectId("525facace4b0c1f5e78753ea"),
"productId" : null,
"device" : "desktop",
"name" : "example name",
"time" : ISODate("2013-10-17T09:23:56.131Z"),
"type" : "hover",
"url" : "www.example.com",
"userAgent" : "curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 openssl/0.9.8r zlib/1.2.5"
}
我跟MongoDB的Java驅動程序的工作,到目前爲止,我做了以下內容:
DBObject query = new BasicDBObject();
query.put("device", new BasicDBObject("$exists", false)); //some entries already have such field
DBCursor cursor = resource.find(query);
cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
Iterator<DBObject> iterator = cursor.iterator();
int size = cursor.count();
然後我用while(iterator.hasNext())
迭代,做一個的if-else與一個巨大的正則表達式,我發現在那裏,並根據結果這樣的if-else我執行類似:
BasicDBObject newDocument = new BasicDBObject("$set", new BasicDBObject().append("device", "desktop")); //of "mobile", depending on the if-else
BasicDBObject searchQuery = new BasicDBObject("_id", id);
resource.getCollection(DatabaseConfiguration.WEBSITE_STATISTICS).update(searchQuery, newDocument);
Howev呃,由於大量的數據(超過500萬條),這需要永遠。
有沒有這樣做的方式與地圖縮小?到目前爲止,我只用MapReduce進行計數,所以我不確定它是否可以用於其他事情。