List of usage examples for com.mongodb WriteResult getN
public int getN()
From source file:com.sitewhere.mongodb.tenant.MongoTenantManagement.java
License:Open Source License
@Override public List<ITenantGroupElement> removeTenantGroupElements(String groupToken, List<ITenantGroupElementCreateRequest> elements) throws SiteWhereException { List<ITenantGroupElement> deleted = new ArrayList<ITenantGroupElement>(); for (ITenantGroupElementCreateRequest request : elements) { BasicDBObject match = new BasicDBObject(MongoTenantGroupElement.PROP_GROUP_TOKEN, groupToken) .append(MongoTenantGroupElement.PROP_TENANT_ID, request.getTenantId()); DBCursor found = getMongoClient().getTenantGroupElementsCollection().find(match); while (found.hasNext()) { DBObject current = found.next(); WriteResult result = MongoPersistence.delete(getMongoClient().getTenantGroupElementsCollection(), current);//from www . j a v a2 s . co m if (result.getN() > 0) { deleted.add(MongoTenantGroupElement.fromDBObject(current)); } } } return deleted; }
From source file:com.socialsky.mods.MongoPersistor.java
License:Apache License
private void doUpdate(Message<JsonObject> message) { String collection = getMandatoryString("collection", message); if (collection == null) { return;/*from w ww . j a v a 2 s . c o m*/ } JsonObject criteriaJson = getMandatoryObject("criteria", message); if (criteriaJson == null) { return; } DBObject criteria = jsonToDBObject(criteriaJson); JsonObject objNewJson = getMandatoryObject("objNew", message); if (objNewJson == null) { return; } DBObject objNew = jsonToDBObject(objNewJson); Boolean upsert = message.body().getBoolean("upsert", false); Boolean multi = message.body().getBoolean("multi", false); DBCollection coll = db.getCollection(collection); WriteConcern writeConcern = WriteConcern.valueOf(getOptionalStringConfig("writeConcern", "")); // Backwards compatibility if (writeConcern == null) { writeConcern = WriteConcern.valueOf(getOptionalStringConfig("write_concern", "")); } if (writeConcern == null) { writeConcern = db.getWriteConcern(); } WriteResult res = coll.update(criteria, objNew, upsert, multi, writeConcern); if (res.getError() == null) { JsonObject reply = new JsonObject(); reply.putNumber("number", res.getN()); sendOK(message, reply); } else { sendError(message, res.getError()); } }
From source file:com.socialsky.mods.MongoPersistor.java
License:Apache License
private void doDelete(Message<JsonObject> message) { String collection = getMandatoryString("collection", message); if (collection == null) { return;/*from w w w .j av a2s. c o m*/ } JsonObject matcher = getMandatoryObject("matcher", message); if (matcher == null) { return; } DBCollection coll = db.getCollection(collection); DBObject obj = jsonToDBObject(matcher); WriteConcern writeConcern = WriteConcern.valueOf(getOptionalStringConfig("writeConcern", "")); // Backwards compatibility if (writeConcern == null) { writeConcern = WriteConcern.valueOf(getOptionalStringConfig("write_concern", "")); } if (writeConcern == null) { writeConcern = db.getWriteConcern(); } WriteResult res = coll.remove(obj, writeConcern); int deleted = res.getN(); JsonObject reply = new JsonObject().putNumber("number", deleted); sendOK(message, reply); }
From source file:com.sorena.write.dao.AbstractFacade.java
public boolean delete(T t) throws Exception { try {/*w w w.ja va 2s .c o m*/ WriteResult result = datastore.delete(t); return (result.getN() != 0); } catch (Exception e) { throw new Exception(); } }
From source file:com.springapp.mvc.dao.impl.WarehouseDaoImpl.java
License:Open Source License
@Override public int deleteById(int code) { Query remove = new Query(Criteria.where("_code").is(code)); WriteResult result = mongoOperations.remove(remove, Warehouse.class, WAREHOUSE_COLLECIONS); return result.getN(); }
From source file:com.tilab.fiware.metaware.dao.impls.mongodb.AlgorithmDao.java
License:Open Source License
/** * Updates the selected algorithm's metadata if exists, otherwise creates a new one. * * @param id the Id of the selected algorithm's metadata to be updated. * @param algorihtm the algorithm's metadata object with the modifications (or the metadata to be * saved).//w ww .jav a 2 s.c om * @return the updated metadata object. */ public Algorithm upsertAlgorithm(String id, Algorithm algorihtm) { log.debug(MSG_DAO_UPSERT + id); if (!ObjectId.isValid(id)) { log.error(MSG_ERR_NOT_VALID_ID); throw new BadRequestException(MSG_ERR_NOT_VALID_ID); } if (algorihtm == null) { log.error(MSG_ERR_NOT_VALID_OBJ); throw new BadRequestException(MSG_ERR_NOT_VALID_OBJ); } // Intercept the possibility to change the Id if (algorihtm.containsField("_id")) { algorihtm.removeField("_id"); } // Set collections DBCollection usersCollection = INSTANCE.getDatasource().getDbCollection(USERS_COLLECTION_NAME); DBCollection departmentsCollection = INSTANCE.getDatasource().getDbCollection(DEPARTMENTS_COLLECTION_NAME); DBCollection companiesCollection = INSTANCE.getDatasource().getDbCollection(COMPANIES_COLLECTION_NAME); usersCollection.setObjectClass(User.class); departmentsCollection.setObjectClass(Department.class); companiesCollection.setObjectClass(Company.class); // Check if the owner field is ok ObjectId ownerId = checkOwner(algorihtm, usersCollection, departmentsCollection, companiesCollection); if (ownerId != null) { algorihtm.setOwnerId(ownerId); } else { log.error(MSG_ERR_NOT_VALID_OWNER_ID); throw new BadRequestException(MSG_ERR_NOT_VALID_OWNER_ID); } // Check if the permissions field is ok List<Permission> permissionsList = checkPermissions(algorihtm, usersCollection, departmentsCollection, companiesCollection); if (permissionsList != null) { algorihtm.setPermissions(permissionsList); } else { log.error(MSG_ERR_NOT_VALID_PERMISSION); throw new BadRequestException(MSG_ERR_NOT_VALID_PERMISSION); } algorithmsCollection = INSTANCE.getDatasource().getDbCollection(ALGORITHMS_COLLECTION_NAME); algorithmsCollection.setObjectClass(Algorithm.class); BasicDBObject query = new BasicDBObject(); query.put("_id", new ObjectId(id)); WriteResult wRes = algorithmsCollection.update(query, algorihtm, true, false); // upply upsert String numUpdates = String.valueOf(wRes.getN()); String jsonMsg; try { jsonMsg = INSTANCE.getObjectMapper().writeValueAsString(algorihtm); log.debug(numUpdates + " algorithm updated: " + jsonMsg); } catch (JsonProcessingException e) { log.error(e, e); } return algorihtm; }
From source file:com.tilab.fiware.metaware.dao.impls.mongodb.AlgorithmDao.java
License:Open Source License
/** * Removes the selected algorithm's metadata. * * @param id the Id of the selected algorithm's metadata. *///from w w w . jav a 2 s. c o m public void deleteAlgorithm(String id) { log.debug(MSG_DAO_DELETE + id); if (!ObjectId.isValid(id)) { log.error(MSG_ERR_NOT_VALID_ID); throw new BadRequestException(MSG_ERR_NOT_VALID_ID); } algorithmsCollection = INSTANCE.getDatasource().getDbCollection(ALGORITHMS_COLLECTION_NAME); BasicDBObject query = new BasicDBObject(); query.put("_id", new ObjectId(id)); WriteResult wRes = algorithmsCollection.remove(query); if (wRes.getN() == 0) { log.error(MSG_ERR_NOT_FOUND); throw new ResourceNotFoundException(); } }
From source file:com.tilab.fiware.metaware.dao.impls.mongodb.CompanyDao.java
License:Open Source License
/** * Update the selected company if exists, otherwise create a new one. * * @param id the Id of the selected company to be updated. * @param company the company object with the modifications (or the company to be saved). * @return the updated company object./*from ww w . j a va 2s. c om*/ */ public Company upsertCompany(String id, Company company) { log.debug(MSG_DAO_UPSERT + id + "."); if (!ObjectId.isValid(id)) { log.error(MSG_ERR_NOT_VALID_ID); throw new BadRequestException(MSG_ERR_NOT_VALID_ID); } if (company == null) { log.error(MSG_ERR_NOT_VALID_OBJ); throw new BadRequestException(MSG_ERR_NOT_VALID_OBJ); } // Intercept the possibility to change the Id if (company.containsField("_id")) { company.removeField("_id"); } companiesCollection = INSTANCE.getDatasource().getDbCollection(COMPANIES_COLLECTION_NAME); companiesCollection.setObjectClass(Company.class); BasicDBObject query = new BasicDBObject(); query.put("_id", new ObjectId(id)); WriteResult wRes = companiesCollection.update(query, company, true, false); // selection criteria, modifications to apply, upsert, multi-document update String jsonMsg; String numUpdates; try { numUpdates = String.valueOf(wRes.getN()); jsonMsg = INSTANCE.getObjectMapper().writeValueAsString(company); log.debug(numUpdates + " company updated: " + jsonMsg); } catch (JsonProcessingException e) { log.error(e, e); } return company; }
From source file:com.tilab.fiware.metaware.dao.impls.mongodb.CompanyDao.java
License:Open Source License
/** * Remove the selected company./* www . j a v a2 s . co m*/ * * @param id the Id of the selected company. */ public void deleteCompany(String id) { log.debug(MSG_DAO_DELETE + id + "."); if (!ObjectId.isValid(id)) { log.error(MSG_ERR_NOT_VALID_ID); throw new BadRequestException(MSG_ERR_NOT_VALID_ID); } companiesCollection = INSTANCE.getDatasource().getDbCollection(COMPANIES_COLLECTION_NAME); BasicDBObject query = new BasicDBObject(); query.put("_id", new ObjectId(id)); WriteResult wRes = companiesCollection.remove(query); if (wRes.getN() == 0) { log.error(MSG_ERR_NOT_FOUND); throw new ResourceNotFoundException(); } }
From source file:com.tilab.fiware.metaware.dao.impls.mongodb.DatasetDao.java
License:Open Source License
/** * Updates the selected dataset's metadata if exists, otherwise creates a new one. * * @param id the Id of the selected dataset's metadata to be updated. * @param dataset the dataset's metadata object with the modifications (or the metadata to be * saved)//from www. j av a2 s . c o m * @return the updated metadata object. */ public Dataset upsertDataset(String id, Dataset dataset) { log.debug(MSG_DAO_UPSERT); if (!ObjectId.isValid(id)) { log.error(MSG_ERR_NOT_VALID_ID); throw new BadRequestException(MSG_ERR_NOT_VALID_ID); } if (dataset == null) { log.error(MSG_ERR_NOT_VALID_OBJ); throw new BadRequestException(MSG_ERR_NOT_VALID_OBJ); } // Intecept the possibility to change the Id if (dataset.containsField("_id")) { dataset.removeField("_id"); } // Set collections DBCollection usersCollection = INSTANCE.getDatasource().getDbCollection(USERS_COLLECTION_NAME); DBCollection departmentsCollection = INSTANCE.getDatasource().getDbCollection(DEPARTMENTS_COLLECTION_NAME); DBCollection companiesCollection = INSTANCE.getDatasource().getDbCollection(COMPANIES_COLLECTION_NAME); usersCollection.setObjectClass(User.class); departmentsCollection.setObjectClass(Department.class); companiesCollection.setObjectClass(Company.class); // Check if the owner field is ok and retrieve the objectId ObjectId ownerId = checkOwner(dataset, usersCollection, departmentsCollection, companiesCollection); if (ownerId != null) { dataset.setOwnerId(ownerId); } else { log.error(MSG_ERR_NOT_VALID_OWNER_ID); throw new BadRequestException(MSG_ERR_NOT_VALID_OWNER_ID); } // Check if the users field is ok and retrieve the objectIds list List<Permission> permissionsList = checkPermissions(dataset, usersCollection, departmentsCollection, companiesCollection); if (permissionsList != null) { dataset.setPermissions(permissionsList); } else { log.error(MSG_ERR_NOT_VALID_PERMISSION); throw new BadRequestException(MSG_ERR_NOT_VALID_PERMISSION); } datasetsCollection = INSTANCE.getDatasource().getDbCollection(DATASETS_COLLECTION_NAME); datasetsCollection.setObjectClass(Dataset.class); BasicDBObject query = new BasicDBObject(); query.put("_id", new ObjectId(id)); WriteResult wRes = datasetsCollection.update(query, dataset, true, false); // selection criteria, modifications to apply, upsert, multi-document String numUpdates = String.valueOf(wRes.getN()); String jsonMsg; try { jsonMsg = INSTANCE.getObjectMapper().writeValueAsString(dataset); log.debug(numUpdates + " dataset updated: " + jsonMsg); } catch (JsonProcessingException e) { log.error(e, e); } return dataset; }