@Override public List<Execution> getActiveTests() { List<Execution> result = new ArrayList<>(); for(Execution execution:collection.find("{status: { $ne: 'ENDED' }}").as(Execution.class)) { result.add(execution); } return result; }
@Override public User getByUsername(String username) { assert username != null; return collection.findOne("{username: #}", username).as(User.class); }
@Override public T save(T entity) { collection.save(entity); return entity; }
/** * Sets the collection. * * @param collection * the new collection */ @JsonIgnore public void setCollection(final MongoCollection collection) { this.collection = collection; // assuming this is called only once after creation, simply save the // entire state collection.save(this); collection.ensureIndex("{ _id: 1}"); collection.ensureIndex("{ _id: 1, timestamp:1 }"); }
private void doInsert(MongoCollection collection, String opQuery, Object[] parameters) throws DataServiceFault { if (opQuery != null) { if (parameters.length > 0) { if (opQuery.equals("#")) { collection.save(JSON.parse(parameters[0].toString())); } else { collection.insert(opQuery, parameters); } } else { collection.insert(opQuery); } } else { throw new DataServiceFault("Mongo insert statements must contain a query"); } }
public void recordCollection(MongoCollection mongoCollection) { String key = getGivenCollectionKey(mongoCollection.getName()); if (givens.containsKey(key)) { return; } Stopwatch stopwatch = Stopwatch.createStarted(); System.out.print("RECORDING " + mongoCollection.getName() + "..."); Iterable<String> items = mongoCollection.find().map(new ResultHandler<String>() { @Override public String map(DBObject result) { return JSON.serialize(result); } }); givens.put(key, new GivenJongoCollection(mongoCollection.getName(), "", " " + Joiner.on("\n ").join(items), ImmutableList.<String>of())); System.out.println(" >> recorded " + mongoCollection.getName() + " -- " + stopwatch.toString()); }
@Override public void remove(ObjectId id) { collection.remove(id); }
@Override public void save(List<? extends T> entities) { this.collection.insert(entities.toArray()); } }
@Override public int _deleteByCriteria(Table table) { Map<String, Object> coditon = new HashMap<String, Object>(); try { QueryCriteria queryCriteria = table.getQueryCriteria(); DB db = database.getDB(); Jongo jongo = new Jongo(db); for(Criteria criteria:queryCriteria.getOredCriteria()){ for(Criterion criterion:criteria.getAllCriteria()){ coditon = buildCriteria(criterion, coditon); } } Find find = jongo.getCollection(queryCriteria.getTable()).find(JsonUtils.objToJson(coditon)); Iterator<Map> iterator = find.as(Map.class).iterator(); while (iterator.hasNext()) { Map<String, Object> map = iterator.next(); if (null != map) { if(map.containsKey("_id")){ jongo.getCollection(table.getTableName()).remove(new ObjectId(String.valueOf(map.get("_id")))); } } } } catch (MongoException e) { LOG.error("mongo delete error", e); } return 1; }
@Override public long countReportNodesByExecutionID(String executionID) { assert executionID != null; return collection.count("{executionID: #}"); }
final Mapper mapper = getMapper(); MongoCollection mongoCollection = new MongoCollection( collection.get().getDBCollection(), new Mapper() { @Override
WriteResult update = jongo.getCollection(tableName).update(new ObjectId(newPropertyObjectKeyValue)).with(query); int wasUpdated = update.getN(); if (wasUpdated == 1) {
MongoCollection col = new Jongo(DbConfigurer.getDB()).getCollection("CODE"); col.getDBCollection().rename("CODE45", true);
@Override public void remove(String username) { collection.remove("{'username':'"+username+"'}"); }
@Override public int _updateByPrimaryKey(Table table) { try { if(table.getParams().containsKey("_id")){ DB db = database.getDB(); Jongo jongo = new Jongo(db); FindOne find = jongo.getCollection(table.getTableName()).findOne(new ObjectId(String.valueOf(table.getParams().get("_id")))); Map<String, Object> map = find.as(Map.class); boolean update = false; if (null != map) { Iterator<String> iter = table.getParams().keySet().iterator(); while (iter.hasNext()) { String key = iter.next(); if (map.containsKey(key) && !"_id".equals(key)) { map.put(key, table.getParams().get(key)); update = true; } } } if(update){ jongo.getCollection(table.getTableName()).save(map); } return 1; } } catch (MongoException e) { LOG.error("mongo update error", e); } return 0; }
@Override public void save(final String query, final Object... queryParams) throws MongoDataException { try { getCollection().insert(query, queryParams); } catch (com.mongodb.DuplicateKeyException ex) { String msg = "Duplicate key for save query " + query + " of type " + clazz.getName() + " with params " + Arrays.toString(queryParams); log.error(msg, ex); throw new DuplicateKeyException(msg, ex); } catch (MongoException ex) { String msg = "Unable to save document by query " + query + " of type " + clazz.getName() + " with params " + Arrays.toString(queryParams); log.error(msg, ex); throw new MongoDataException(msg, ex); } }
private Iterator<Long> doCount(MongoCollection collection, String opQuery, Object[] parameters) { long count; if (opQuery != null) { if (parameters.length > 0) { count = collection.count(opQuery, parameters); } else { count = collection.count(opQuery); } } else { count = collection.count(); } List<Long> countResult = new ArrayList<>(); countResult.add(count); return countResult.iterator(); }