import domain.SiteDocField; import domain.PageDocField; import core.db.DbConfig; import core.db.DoDbSingle; // MongoDB import com.mongodb.BasicDBObject; import com.mongodb.DBCursor; import com.mongodb.DBCollection; /** * Change the data type of the values in the document. This is a util run * from the command line. * * @author Ryan Nitz */ public final class ChangeDataType { /** * Convert the data type. * @param pArgs The arguments. * @throws Exception */ public static void main(final String [] pArgs) throws Exception { final DbConfig config = new DbConfig("something", "localhost"); final DoDbSingle db = new DoDbSingle(config); convertSites( db.getDb().getCol("sites").find(), db.getDb().getCol("sites"), db); convertPages( db.getDb().getCol("pages").find(), db.getDb().getCol("pages"), db); } /** * Convert the sites. * @param pCur The cursor. * @param pCol The collection. */ private static void convertSites( final DBCursor pCur, final DBCollection pCol, final DoDbSingle pDb) { while (pCur.hasNext()) { final BasicDBObject doc = (BasicDBObject)pCur.next(); final byte [] id = (byte[])doc.get(SiteDocField.ID.key); if (doc.containsField(SiteDocField.HAS_AD_INFO_PAGE.key)) { final Object obj = doc.get(SiteDocField.HAS_AD_INFO_PAGE.key); if (obj != null) { double value = convertToDouble(obj); // Does not work pCol.update(new BasicDBObject(SiteDocField.ID.key, id), new BasicDBObject("$set", new BasicDBObject(SiteDocField.HAS_AD_INFO_PAGE.key, value)), false, false); // Works //doc.put(SiteDocField.HAS_AD_INFO_PAGE.key, value); //pCol.save(doc); } else { doc.removeField(SiteDocField.HAS_AD_INFO_PAGE.key); pCol.save(doc); } pDb.getDb().getLastError(); } } } /** * Convert the pages. * @param pCur The cursor. * @param pCol The collection. */ private static void convertPages( final DBCursor pCur, final DBCollection pCol, final DoDbSingle pDb) { while (pCur.hasNext()) { final BasicDBObject doc = (BasicDBObject)pCur.next(); final byte [] id = (byte[])doc.get(PageDocField.ID.key); if (doc.containsField(PageDocField.HAS_GOOGLE_TEXT_ADS.key)) { final Object obj = doc.get(PageDocField.HAS_GOOGLE_TEXT_ADS.key); if (obj != null) { double value = convertToDouble(obj); // Does not work pCol.update(new BasicDBObject(PageDocField.ID.key, id), new BasicDBObject("$set", new BasicDBObject(PageDocField.HAS_GOOGLE_TEXT_ADS.key, value)), false, false); // Works //doc.put(PageDocField.HAS_GOOGLE_TEXT_ADS.key, value); //pCol.save(doc); } else { doc.removeField(PageDocField.HAS_GOOGLE_TEXT_ADS.key); pCol.save(doc); } pDb.getDb().getLastError(); } } } /** * Return the value as a double. * @param pObj The object. * @return The value as a double. */ private static double convertToDouble( final Object pObj) { if (pObj instanceof Double) { return (Double)pObj; } else if (pObj instanceof Boolean) { return (((Boolean)pObj)) ? (double)1 : (double)0; } else if (pObj instanceof Long) { return (double)((Long)pObj); } else if (pObj instanceof Integer) { return (double)((Integer)pObj); } else { throw new IllegalStateException("No handler for: " + pObj.getClass().getName()); } } }