本文整理匯總了Java中com.mongodb.MongoException類的典型用法代碼示例。如果您正苦於以下問題:Java MongoException類的具體用法?Java MongoException怎麽用?Java MongoException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
MongoException類屬於com.mongodb包,在下文中一共展示了MongoException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getAllDocuments
import com.mongodb.MongoException; //導入依賴的package包/類
/**
* This method retrieve all the document(s)
*/
@Override
public void getAllDocuments() {
MongoDatabase db = null;
MongoCollection collection = null;
try {
db = client.getDatabase(mongo.getDataBase());
collection = db.getCollection(mongo.getSampleCollection());
FindIterable<Document> docs = collection.find(); //SELECT * FROM sample;
for (Document doc : docs) {
log.info(doc.getString("name"));
}
} catch (MongoException | ClassCastException e) {
log.error("Exception occurred while insert Value using **BasicDBObject** : " + e, e);
}
}
示例2: deleteManyDocument
import com.mongodb.MongoException; //導入依賴的package包/類
/**
* This is deleted delete all document(s), which is matched
*/
@Override
public void deleteManyDocument() {
MongoDatabase db = null;
MongoCollection collection = null;
Bson query = null;
try {
db = client.getDatabase(mongo.getDataBase());
collection = db.getCollection(mongo.getSampleCollection());
query = lt("age", 20);
DeleteResult result = collection.deleteMany(query);
if (result.wasAcknowledged()) {
log.info("Document deleted successfully \nNo of Document(s) Deleted : "
+ result.getDeletedCount());
}
} catch (MongoException e) {
log.error("Exception occurred while delete Many Document : " + e, e);
}
}
示例3: updateDocumentWithCurrentDate
import com.mongodb.MongoException; //導入依賴的package包/類
/**
* This method update document with lastmodified properties
*/
@Override
public void updateDocumentWithCurrentDate() {
MongoDatabase db = null;
MongoCollection collection = null;
Bson filter = null;
Bson query = null;
try {
db = client.getDatabase(mongo.getDataBase());
collection = db.getCollection(mongo.getSampleCollection());
filter = eq("name", "Sundar");
query = combine(set("age", 23), set("gender", "Male"),
currentDate("lastModified"));
UpdateResult result = collection.updateOne(filter, query);
log.info("Update with date Status : " + result.wasAcknowledged());
log.info("No of Record Modified : " + result.getModifiedCount());
} catch (MongoException e) {
log.error("Exception occurred while update Many Document with Date : " + e, e);
}
}
示例4: insertUsingDocument
import com.mongodb.MongoException; //導入依賴的package包/類
/**
* This method insert the document using Document object
*/
@Override
public void insertUsingDocument() {
MongoDatabase db = null;
MongoCollection collection = null;
try {
db = client.getDatabase(mongo.getDataBase());
collection = db.getCollection(mongo.getSampleCollection());
Document obj1 = new Document();
obj1.put("name", "Sivaraman");
obj1.put("age", 23);
obj1.put("gender", "male");
collection.insertOne(obj1);
log.info("Document Insert Successfully using Document Obj...");
} catch (MongoException | ClassCastException e) {
log.error("Exception occurred while insert Value using **Document** : " + e, e);
}
}
示例5: deleteOneDocument
import com.mongodb.MongoException; //導入依賴的package包/類
/**
* This is delete the single document, which is first matched
*/
@Override
public void deleteOneDocument() {
MongoDatabase db = null;
MongoCollection collection = null;
Bson query = null;
try {
db = client.getDatabase(mongo.getDataBase());
collection = db.getCollection(mongo.getSampleCollection());
query = eq("name", "sundar");
DeleteResult result = collection.deleteMany(query);
if (result.wasAcknowledged()) {
log.info("Single Document deleted successfully \nNo of Document Deleted : " + result.getDeletedCount());
}
} catch (MongoException e) {
log.error("Exception occurred while delete Single Document : " + e, e);
}
}
示例6: updateManyDocument
import com.mongodb.MongoException; //導入依賴的package包/類
/**
* This method update all the matches document
*/
@Override
public void updateManyDocument() {
MongoDatabase db = null;
MongoCollection collection = null;
Bson filter = null;
Bson query = null;
try {
db = client.getDatabase(mongo.getDataBase());
collection = db.getCollection(mongo.getSampleCollection());
filter = eq("name", "Sundar");
query = combine(set("age", 23), set("gender", "Male"));
UpdateResult result = collection.updateMany(filter, query);
log.info("UpdateMany Status : " + result.wasAcknowledged());
log.info("No of Record Modified : " + result.getModifiedCount());
} catch (MongoException e) {
log.error("Exception occurred while update Many Document : " + e, e);
}
}
示例7: insertUsingMap
import com.mongodb.MongoException; //導入依賴的package包/類
/**
* This method insert the document using Map
*/
@Override
public void insertUsingMap() {
MongoDatabase db = null;
MongoCollection collection = null;
try {
db = client.getDatabase(mongo.getDataBase());
collection = db.getCollection(mongo.getSampleCollection());
final Map<String, Object> empMap = new HashMap<>();
empMap.put("_id", new Random().nextInt(999));
empMap.put("name", "Vel");
empMap.put("age", 25);
empMap.put("desicnation", "Java Developer");
empMap.put("gender", "Male");
empMap.put("salary", "10000");
log.info("Employ Details : " + empMap);
collection.insertOne(new Document(empMap));
log.info("Document Insert Successfully using Map...");
} catch (MongoException | ClassCastException e) {
log.error("Exception occurred while insert Value using **UsingMap** : " + e, e);
}
}
示例8: insertSingleDocument
import com.mongodb.MongoException; //導入依賴的package包/類
/**
* This method insert the single document
*/
@Override
public void insertSingleDocument() {
MongoDatabase db = null;
MongoCollection collection = null;
try {
db = client.getDatabase(mongo.getDataBase());
collection = db.getCollection(mongo.getSampleCollection());
Document canvas = new Document("item", "canvas").append("qty", 100)
.append("tags", singletonList("cotton"));
Document size = new Document("h", 28).append("w", 35.5).append(
"uom", "cm");
canvas.append("size", size);
collection.insertOne(canvas);
log.info("Single Document Insert Successfully...");
} catch (MongoException | ClassCastException e) {
log.error("Exception occurred while insert **Single Document** : " + e, e);
}
}
示例9: activate
import com.mongodb.MongoException; //導入依賴的package包/類
@Transactional
public Mono<Void> activate(String uuid) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication.getPrincipal() instanceof AuthenticatedUser) {
AuthenticatedUser authenticatedUser = (AuthenticatedUser) authentication.getPrincipal();
String errorMsg = String.format("Unable to retrieve current user : %s", authenticatedUser.getUsername());
return userRepository.findByUsernameAndTemporaryCode(authenticatedUser.getUsername(), uuid)
.flatMap(user -> {
user.setTemporaryCode(null);
return authorityService.findDefaultOrCreate().map(authority -> {
user.getAuthorities().add(authority);
return user;
});
})
.switchIfEmpty(Mono.error(new MongoException(errorMsg)))
.flatMap(userRepository::save)
.then();
}
return Mono.error(new IllegalStateException("Wrong principal type"));
}
示例10: load
import com.mongodb.MongoException; //導入依賴的package包/類
@Override
public List<String> load(String key) throws Exception {
if (!DATABASES.equals(key)) {
throw new UnsupportedOperationException();
}
try {
List<String> dbNames = new ArrayList<>();
client.listDatabaseNames().into(dbNames);
return dbNames;
} catch (MongoException me) {
logger.warn("Failure while loading databases in Mongo. {}",
me.getMessage());
return Collections.emptyList();
} catch (Exception e) {
throw new DrillRuntimeException(e.getMessage(), e);
}
}
示例11: remove
import com.mongodb.MongoException; //導入依賴的package包/類
/**
* {@inheritDoc}
*/
public void remove(String id) throws IOException {
/* build up the query, looking for all sessions with this app context property and id */
BasicDBObject sessionQuery = new BasicDBObject();
sessionQuery.put(idProperty, id);
sessionQuery.put(appContextProperty, this.getName());
/* remove all sessions for this context and id */
try {
this.collection.remove(sessionQuery);
if (isDebugEnabled()) {
getLog().debug("removed session " + id + " (query: " + sessionQuery + ")");
}
} catch (MongoException e) {
/* for some reason we couldn't remove the data */
getLog().error("Unable to remove sessions for [" + id + ":" + this.getName() + "] from MongoDB", e);
throw e;
}
}
示例12: clear
import com.mongodb.MongoException; //導入依賴的package包/類
/**
* {@inheritDoc}
*/
public void clear() throws IOException {
/* build up the query, looking for all sessions with this app context property */
BasicDBObject sessionQuery = new BasicDBObject();
sessionQuery.put(appContextProperty, this.getName());
/* remove all sessions for this context */
try {
this.collection.remove(sessionQuery);
getLog().debug("removed sessions (query: " + sessionQuery + ")");
} catch (MongoException e) {
/* for some reason we couldn't save the data */
getLog().error("Unable to remove sessions for [" + this.getName() + "] from MongoDB", e);
throw e;
}
}
示例13: createDatabase
import com.mongodb.MongoException; //導入依賴的package包/類
public DB createDatabase(String databaseName) throws MongoServiceException {
try {
DB db = client.getDB(databaseName);
// save into a collection to force DB creation.
DBCollection col = db.createCollection("foo", null);
BasicDBObject obj = new BasicDBObject();
obj.put("foo", "bar");
col.insert(obj);
// drop the collection so the db is empty
// col.drop();
return db;
} catch (MongoException e) {
// try to clean up and fail
try {
deleteDatabase(databaseName);
} catch (MongoServiceException ignore) {}
throw handleException(e);
}
}
示例14: connectionTest
import com.mongodb.MongoException; //導入依賴的package包/類
public static boolean connectionTest(MongoDBConfig mongoDBConfig) {
Logging.disableMongoDBLogging();
boolean success = true;
MongoClient mongoClient = null;
try {
mongoClient = new MongoClient(new MongoClientURI("mongodb://" + mongoDBConfig.getIp() + ":" + mongoDBConfig.getPort()));
mongoClient.getDatabaseNames();
} catch (MongoException e) {
success = false;
} finally {
if (mongoClient != null) {
mongoClient.close();
}
Logging.enableMongoDBLogging();
}
return success;
}
示例15: reportGauge
import com.mongodb.MongoException; //導入依賴的package包/類
private void reportGauge(final String name, final Gauge gauge, final Date timestamp) {
final DBCollection coll = db.getCollection("metric_gauge");
final Object value = gauge.getValue();
if (value == null) {
// skip report
return;
}
if (!String.class.equals(value.getClass())) {
final GaugeEntity entity = new GaugeEntity();
entity.setName(prefix(name));
entity.setTimestamp(timestamp);
entity.setValue(value);
try {
coll.save(entity.toDBObject());
} catch (MongoException e) {
LOGGER.warn("Unable to report gauge {}", name, e);
}
}
}