List of usage examples for com.mongodb DBObject put
Object put(String key, Object v);
From source file:com.bosscs.spark.mongodb.config.MongoDeepJobConfig.java
License:Apache License
/** * {@inheritDoc}// ww w . ja va 2 s .com */ @Override public MongoDeepJobConfig<T> inputColumns(String... columns) { DBObject bsonFields = fields != null ? fields : new BasicDBObject(); boolean isIdPresent = false; for (String column : columns) { if (column.trim().equalsIgnoreCase("_id")) { isIdPresent = true; } bsonFields.put(column.trim(), 1); } if (!isIdPresent) { bsonFields.put("_id", 0); } fields = bsonFields; return this; }
From source file:com.bosscs.spark.mongodb.utils.UtilMongoDB.java
License:Apache License
/** * converts from an entity class with deep's anotations to BsonObject. * * @param t an instance of an object of type T to convert to BSONObject. * @return the provided object converted to BSONObject. * @throws IllegalAccessException the illegal access exception * @throws IllegalAccessException the instantiation exception * @throws IllegalAccessException the invocation target exception *///from w w w. j av a 2 s . c om public static <T> DBObject getBsonFromObject(T t) throws IllegalAccessException, InstantiationException, InvocationTargetException { Field[] fields = AnnotationUtils.filterDeepFields(t.getClass()); DBObject bson = new BasicDBObject(); for (Field field : fields) { Method method = Utils.findGetter(field.getName(), t.getClass()); Object object = method.invoke(t); if (object != null) { if (Collection.class.isAssignableFrom(field.getType())) { Collection c = (Collection) object; Iterator iterator = c.iterator(); List innerBsonList = new ArrayList<>(); while (iterator.hasNext()) { innerBsonList.add(getBsonFromObject(iterator.next())); } bson.put(AnnotationUtils.deepFieldName(field), innerBsonList); } else if (IType.class.isAssignableFrom(field.getType())) { bson.put(AnnotationUtils.deepFieldName(field), getBsonFromObject((IType) object)); } else { bson.put(AnnotationUtils.deepFieldName(field), object); } } } return bson; }
From source file:com.bosscs.spark.mongodb.utils.UtilMongoDB.java
License:Apache License
/** * converts from and entity class with deep's anotations to BsonObject * * @param cells the cells//www.j a va 2s . co m * @return bson from cell */ public static DBObject getDBObjectFromCell(Cells cells) { DBObject bson = new BasicDBObject(); for (Cell cell : cells) { if (cell.getValue() != null) { if (Collection.class.isAssignableFrom(cell.getCellValue().getClass())) { Collection c = (Collection) cell.getCellValue(); Iterator iterator = c.iterator(); List<Object> innerBsonList = new ArrayList<>(); while (iterator.hasNext()) { Object currentO = iterator.next(); if (currentO instanceof Cells) { innerBsonList.add(getDBObjectFromCell((Cells) currentO)); } else { innerBsonList.add(currentO); } } bson.put(cell.getCellName(), innerBsonList); } else if (Cells.class.isAssignableFrom(cell.getCellValue().getClass())) { bson.put(cell.getCellName(), getDBObjectFromCell((Cells) cell.getCellValue())); } else { bson.put(cell.getCellName(), cell.getCellValue()); } } } return bson; }
From source file:com.bryanreinero.purchase.PurchaseDAO.java
License:Open Source License
private static DBObject toDBObject(Purchase purchase) { DBObject object = new BasicDBObject(); object.put("_id", purchase.getId()); object.put("state", purchase.getState().toString()); object.put("buyer", purchase.getBuyer()); object.put("seller", purchase.getSeller()); object.put("value", purchase.getValue()); return object; }
From source file:com.bryanreinero.purchase.PurchaseDAO.java
License:Open Source License
@Override public void updatePrincipal(String principal, Integer transactionId, int value) throws TransactionException { DBObject q = new BasicDBObject("_id", principal); q.put("pendingTransactions", new BasicDBObject("$ne", transactionId)); DBObject o = new BasicDBObject("$inc", new BasicDBObject("balance", value)); o.put("$push", new BasicDBObject("pendingTransactions", transactionId)); try {// ww w .ja va 2 s. co m WriteResult result = principals.update(q, o); if (!result.getLastError().ok()) throw new TransactionException("Pricipal update failure: " + result.getError()); } catch (MongoException e) { throw new TransactionException("Pricipal update failure", e); } }
From source file:com.bryanreinero.purchase.PurchaseDAO.java
License:Open Source License
@Override public void cancellTransaction(String principal, Integer transactionId, int value) throws TransactionException { DBObject q = new BasicDBObject("_id", principal); q.put("pendingTransactions", transactionId); DBObject o = new BasicDBObject("$pull", new BasicDBObject("pendingTransactions", transactionId)); o.put("$inc", new BasicDBObject("balance", value)); try {/*w ww. ja v a 2 s. c o m*/ WriteResult result = principals.update(q, o); if (!result.getLastError().ok()) throw new TransactionException("Pricipal update failure: " + result.getError()); } catch (MongoException e) { throw new TransactionException("Pricipal update failure", e); } }
From source file:com.bugull.mongo.BuguDao.java
License:Apache License
private void initCollection(String name) { DB db = null;/*from www.j av a2s . c o m*/ try { db = BuguConnection.getInstance().getDB(); } catch (DBConnectionException ex) { logger.error(ex.getMessage(), ex); } Entity entity = clazz.getAnnotation(Entity.class); //if capped if (entity.capped() && !db.collectionExists(name)) { DBObject options = new BasicDBObject("capped", true); long capSize = entity.capSize(); if (capSize != Default.CAP_SIZE) { options.put("size", capSize); } long capMax = entity.capMax(); if (capMax != Default.CAP_MAX) { options.put("max", capMax); } coll = db.createCollection(name, options); } else { coll = db.getCollection(name); } //for @EnsureIndex EnsureIndex ei = clazz.getAnnotation(EnsureIndex.class); if (ei != null) { List<DBIndex> list = MapperUtil.getDBIndex(ei.value()); for (DBIndex dbi : list) { coll.createIndex(dbi.getKeys(), dbi.getOptions()); } } }
From source file:com.bugull.mongo.BuguDao.java
License:Apache License
/** * Check if any entity with id already exists * @param id the id value to check/*from w w w . ja va 2s . c om*/ * @return */ public boolean exists(String id) { DBObject query = new BasicDBObject(); query.put(Operator.ID, IdUtil.toDbId(clazz, id)); return coll.findOne(query) != null; }
From source file:com.bugull.mongo.BuguDao.java
License:Apache License
/** * Find a single document by id/*from w w w . ja v a2s . com*/ * @param id * @return */ public T findOne(String id) { DBObject dbo = new BasicDBObject(); dbo.put(Operator.ID, IdUtil.toDbId(clazz, id)); DBObject result = coll.findOne(dbo); return MapperUtil.fromDBObject(clazz, result); }
From source file:com.bugull.mongo.BuguQuery.java
License:Apache License
private void append(String key, String op, Object value) { if (op == null) { condition.put(key, value);//from w w w .j a v a 2 s . c o m return; } Object obj = condition.get(key); DBObject dbo = null; if (!(obj instanceof DBObject)) { dbo = new BasicDBObject(op, value); condition.put(key, dbo); } else { dbo = (DBObject) condition.get(key); dbo.put(op, value); } }