Created
June 27, 2014 12:49
Update fixed number of MongoDB records with batchId
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UpdateMongoBatchId { | |
public static void main(String[] args) { | |
Integer batchId = new Integer(args[0]); | |
try { | |
Mongo mongo = new Mongo("10.x.x.x", 27017); | |
DB db = mongo.getDB("dbname"); | |
DBCollection coll1 = db.getCollection("collname"); | |
// MongoDB find conditions | |
BasicDBObject searchQuery = new BasicDBObject(); | |
searchQuery.put("batchId", null); | |
BasicDBObject searchFields = new BasicDBObject(); | |
BasicDBObject sortOrder = new BasicDBObject(); | |
sortOrder.put("systemTime", 1); | |
DBObject currDocument; | |
DBCursor cursor = coll1.find(searchQuery).sort(sortOrder).limit(MongoVariables.BATCH_SIZE); | |
try { | |
while (cursor.hasNext()) { | |
currDocument = cursor.next(); | |
currDocument.put("batchId", batchId); | |
coll1.save(currDocument); | |
} | |
} catch (Exception e) { | |
// TODO: handle exception | |
} finally { | |
cursor.close(); | |
} | |
System.out.println("Updated batchId to MongoDB"); | |
} catch (Exception e) { | |
// TODO: handle exception | |
} finally { | |
if (mongo != null) { | |
mongo.close(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment