Refine search
@Override public <T extends Persisted> int destroyAll(Class<T> modelClass) { return collection(modelClass).remove(new BasicDBObject()).getN(); }
/** * @return true if updated, false if inserted or none effected */ public boolean getUpdatedExisting() { return wr.isUpdateOfExisting(); }
@Override public void update(KeycloakSession session) { BasicDBList orArgs = new BasicDBList(); orArgs.add(new BasicDBObject("type", UserCredentialModel.PASSWORD)); orArgs.add(new BasicDBObject("type", UserCredentialModel.PASSWORD_HISTORY)); BasicDBObject elemMatch = new BasicDBObject("$or", orArgs); elemMatch.put("algorithm", new BasicDBObject("$exists", false)); BasicDBObject query = new BasicDBObject("credentials", new BasicDBObject("$elemMatch", elemMatch)); BasicDBObject update = new BasicDBObject("$set", new BasicDBObject("credentials.$.algorithm", Pbkdf2PasswordHashProvider.ID)); DBCollection users = db.getCollection("users"); // Not sure how to do in single query int countModified = 1; while (countModified > 0) { WriteResult wr = users.update(query, update, false, true); countModified = wr.getN(); log.debugf("%d credentials modified in current iteration during upgrade to 1.8", countModified); } } }
DBCollection patients = db.getCollection("Patients"); DBObject update = new BasicDBObject().append("_id", new ObjectId("...")); long collectionCount = patients.count(); System.out.println(String.format("Collection count: %s", collectionCount)); long count = patients.count(update); System.out.println(String.format("Count for query: %s", count)); BasicDBObject doc = new BasicDBObject(); doc.put("name", "seshu"); WriteResult writeResult = patients.update(update, doc); System.out.println(String.format("Updated %s records", writeResult.getN())); DBObject updated = patients.findOne(update); System.out.println(updated);
throw new IllegalArgumentException(MongoMetadataConstants.ERR_NEW_STATUS_IS_NULL); BasicDBObject query = new BasicDBObject(LITERAL_ID, entityName + BSONParser.DELIMITER_ID + version); Error.push("setMetadataStatus(" + entityName + ":" + version + ")"); try { schema.setStatus(newStatus); query = new BasicDBObject(LITERAL_ID, md.get(LITERAL_ID)); WriteResult result = new UpdateCommand(collection, query, (DBObject) mdParser.convert(schema), false, false).execute(); String error = result.getError(); if (error != null) { throw Error.get(MongoMetadataConstants.ERR_DB_ERROR, error);
public void processExpires() { BasicDBObject query = new BasicDBObject(); long olderThan = System.currentTimeMillis() - (getMaxInactiveInterval() * 1000); log.fine("Looking for sessions less than for expiry in Mongo : " + olderThan); query.put("lastmodified", new BasicDBObject("$lt", olderThan)); try { WriteResult result = getCollection().remove(query); log.fine("Expired sessions : " + result.getN()); } catch (IOException e) { log.log(Level.SEVERE, "Error cleaning session in Mongo Session Store", e); } }
@Override public boolean remove(String recordId) { Preconditions.checkArgument(StringUtils.isNotBlank(recordId)); BasicDBObject query = new BasicDBObject(); query.append(F_MONGO_ID, recordId); logger.debug("executing mongo remove: "+ query); WriteResult result = mongoTemplate.getCollection(collectionName).remove(query); return (result.getN() > 0); }
@Override public void releaseLock() { DBObject query = new BasicDBObject("locked", true); BasicDBObject update = new BasicDBObject("locked", false); update.append("_id", 1); update.append("lockedBy", null); update.append("lockedSince", null); try { WriteResult wr = db.getCollection(DB_LOCK_COLLECTION).update(query, update, true, false); if (wr.getN() > 0) { factory.setHasLock(false); logger.debugf("Successfully released DB lock"); } else { logger.warnf("Attempt to release DB lock, but nothing was released"); } } catch (DuplicateKeyException dke) { logger.debugf("Failed release lock. Reason: %s", dke.getMessage()); } }
DBCollection coll = db.getCollection("mycollection"); BasicDBObject doc = new BasicDBObject("name", "MongoDB") .append("type", "database") .append("count", 1); WriteResult insert = coll.insert(doc); String msg = ""; if(insert.getError() == null){ msg = "insert into: " + collection.toString() +" ; Object " + q.toString()); //log the message } else { msg = "ERROR by insert into: " + collection.toString() +" ; Object " + q.toString()); msg = msg + " Error message: " + insert.getError(); } //log the message
@Override public void removeEntity(String entityName) { if (entityName == null || entityName.length() == 0) { throw new IllegalArgumentException(LITERAL_ENTITY_NAME); } // All versions must be disabled. Search for a schema that is not disabled DBObject query = new BasicDBObject(LITERAL_NAME, entityName). append(LITERAL_VERSION, new BasicDBObject("$exists", 1)). append(LITERAL_STATUS_VALUE, new BasicDBObject("$ne", MetadataParser.toString(MetadataStatus.DISABLED))); LOGGER.debug("Checking if there are entity versions that are not disabled: {}", query); DBObject result = new FindOneCommand(collection, query).execute(); if (result != null) { LOGGER.debug("There is at least one enabled version {}", result); throw Error.get(MongoMetadataConstants.ERR_CANNOT_DELETE, entityName); } LOGGER.warn("All versions of {} are disabled, deleting {}", entityName, entityName); query = new BasicDBObject(LITERAL_ID, Pattern.compile(entityName + "\\" + BSONParser.DELIMITER_ID + ".*")); LOGGER.debug("Removal query:{}", query); try { WriteResult r = new RemoveCommand(collection, query).execute(); LOGGER.debug("Removal result:{}", r); String error = r.getError(); if (error != null) { throw Error.get(MongoMetadataConstants.ERR_DB_ERROR, error); } } catch (Exception e) { LOGGER.error("Error during delete", e); throw Error.get(MongoMetadataConstants.ERR_DB_ERROR, e.toString()); } }
public void delete(DBCollection storage, KvEntity entity) { DBObject query = new BasicDBObject(); query.put("_id", entity.id); query.put("version", entity.version); WriteResult result = storage.remove(query); result.getLastError().throwOnError(); if (result.getN()==0) throw new RuntimeException(); } }
public KvEntity update(DBCollection storage, KvEntity entity) { int version = (Integer)entity.version; DBObject query = new BasicDBObject(); query.put("_id", entity.id); query.put("version", version); DBObject record = new BasicDBObject(); record.put("version", version+1); record.put("value", entity.value); record.put("updated", entity.updated); record.put("tx", entity.tx); WriteResult result = storage.update(query, record); result.getLastError().throwOnError(); if (result.getN()==0) throw new RuntimeException(); entity.version = version+1; return entity; }
public KvEntity create(DBCollection storage, DBObject value) { KvEntity entity = new KvEntity(); entity.version = 0; entity.value = value; entity.updated = null; entity.tx = null; DBObject record = new BasicDBObject(); record.put("version", entity.version); record.put("value", entity.value); record.put("updated", entity.updated); record.put("tx", entity.tx); storage.insert(record).getLastError().throwOnError(); entity.id = record.get("_id"); return entity; }
@Override public boolean delete(ObjectId id) { DBObject query = new BasicDBObject(); query.put("oid", id.toString()); return collection.remove(query).getLastError().ok(); }
@Override public boolean put(final RevObject object) { DBObject query = new BasicDBObject(); query.put("oid", object.getId().toString()); DBObject record = toDocument(object); return collection.update(query, record, true, false).getLastError().ok(); }
@Override protected void mark(BlockId blockId) throws Exception { if (minLastModified == 0) { return; } String id = StringUtils.convertBytesToHex(blockId.getDigest()); DBObject query = getBlobQuery(id, minLastModified); DBObject update = new BasicDBObject("$set", new BasicDBObject(MongoBlob.KEY_LAST_MOD, System.currentTimeMillis())); WriteResult writeResult = getBlobCollection().update(query, update); if (writeResult.getError() != null) { LOG.error("Mark failed for blob %s: %s", id, writeResult.getError()); } }
@Override public boolean put(K row, T value, long version) throws AvroBaseException { BasicDBObject ro = b("$set", r(row)); ro.put("$set", b("value", getDBObject(value))); ro.put("$inc", b("version", 1)); BasicDBObject query = r(row); query.put("version", version); return rows.update(query, ro, false, false).getN() == 1; }
/** * Handle update functions. * * @param query * the query * @param update * the update * @param collName * the coll name * @return the int */ public int handleUpdateFunctions(BasicDBObject query, BasicDBObject update, String collName) { DBCollection collection = mongoDb.getCollection(collName); KunderaCoreUtils.printQuery("Update collection:" + query, showQuery); WriteResult result = null; try { result = collection.update(query, update); } catch (MongoException ex) { return -1; } if (result.getN() <= 0) return -1; return result.getN(); }
/** * @return number of affected documents */ protected int getN() { return wr.getN(); }