Skip to content

Instantly share code, notes, and snippets.

@rishav-rohit
Created June 27, 2014 12:49
Show Gist options
  • Save rishav-rohit/064a940c926b66836cf3 to your computer and use it in GitHub Desktop.
Save rishav-rohit/064a940c926b66836cf3 to your computer and use it in GitHub Desktop.
Update fixed number of MongoDB records with batchId
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