本文整理汇总了Java中com.mongodb.MongoCommandException类的典型用法代码示例。如果您正苦于以下问题:Java MongoCommandException类的具体用法?Java MongoCommandException怎么用?Java MongoCommandException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MongoCommandException类属于com.mongodb包,在下文中一共展示了MongoCommandException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: MongoResultService
import com.mongodb.MongoCommandException; //导入依赖的package包/类
/**
* Construct a test result provider against a Mongo database and given collection name
*/
public MongoResultService(DB db, String collection)
{
try
{
this.collection = db.createCollection(collection, new BasicDBObject());
checkIndexes = true;
}
catch (MongoCommandException e)
{
if (!db.collectionExists(collection))
{
// The collection is really not there
throw e;
}
// Someone else created it
this.collection = db.getCollection(collection);
this.checkIndexes = false;
}
}
示例2: insertRecord
import com.mongodb.MongoCommandException; //导入依赖的package包/类
@Override
public boolean insertRecord(LockConfiguration lockConfiguration) {
Bson update = combine(
setOnInsert(LOCK_UNTIL, Date.from(lockConfiguration.getLockAtMostUntil())),
setOnInsert(LOCKED_AT, now()),
setOnInsert(LOCKED_BY, hostname)
);
try {
Document result = getCollection().findOneAndUpdate(
eq(ID, lockConfiguration.getName()),
update,
new FindOneAndUpdateOptions().upsert(true)
);
return result == null;
} catch (MongoCommandException e) {
if (e.getErrorCode() == 11000) { // duplicate key
// this should not normally happen, but it happened once in tests
return false;
} else {
throw e;
}
}
}
示例3: DataReportServiceImpl
import com.mongodb.MongoCommandException; //导入依赖的package包/类
/**
* Constructor
*
* @param db
* (DB, required) the database to use
*/
public DataReportServiceImpl(DB db)
{
if (null == db)
{
throw new IllegalArgumentException("'db' is mandatory!");
}
try
{
// create collection with no options
this.collectionExtraData = db.createCollection(COLLECTION_EXTRA_DATA, null);
this.collectionDescription = db.createCollection(COLLECTION_EXTRA_DATA_DESCRIPTION, null);
}
catch (MongoCommandException e)
{
// try to get collection anyway - if not there, re-throw
if (!db.collectionExists(COLLECTION_EXTRA_DATA) || !db.collectionExists(COLLECTION_EXTRA_DATA_DESCRIPTION))
{
throw e;
}
this.collectionExtraData = db.getCollection(COLLECTION_EXTRA_DATA);
this.collectionDescription = db.getCollection(COLLECTION_EXTRA_DATA_DESCRIPTION);
}
}
示例4: findAndModify
import com.mongodb.MongoCommandException; //导入依赖的package包/类
@Test
public void findAndModify() {
getMorphia().map(DocumentValidation.class);
getDs().enableDocumentValidation();
getDs().save(new DocumentValidation("Harold", 100, new Date()));
Query<DocumentValidation> query = getDs().find(DocumentValidation.class);
UpdateOperations<DocumentValidation> updates = getDs().createUpdateOperations(DocumentValidation.class)
.set("number", 5);
FindAndModifyOptions options = new FindAndModifyOptions()
.bypassDocumentValidation(false);
try {
getDs().findAndModify(query, updates, options);
fail("Document validation should have complained");
} catch (MongoCommandException e) {
// expected
}
options.bypassDocumentValidation(true);
getDs().findAndModify(query, updates, options);
Assert.assertNotNull(query.field("number").equal(5).get());
}
示例5: MongoRequestRepository
import com.mongodb.MongoCommandException; //导入依赖的package包/类
public MongoRequestRepository(MongoDatabase database, Long minutesToExpire) {
this.requests = database.getCollection("requests");
try {
this.requests.dropIndex("createdAt_1");
logger.info("Dropped index 'createdAt', creating a new one.");
} catch (MongoCommandException e) {
logger.info("Index for 'createdAt' doesn't exist, creating index.");
}
this.requests.createIndex(new Document("createdAt", 1),
new IndexOptions().expireAfter(minutesToExpire, TimeUnit.MINUTES));
this.gson = new Gson();
}
示例6: initializeCollection
import com.mongodb.MongoCommandException; //导入依赖的package包/类
public static MongoCollection<Document> initializeCollection(final MongoNamespace namespace) throws Throwable {
MongoDatabase database = getMongoClient().getDatabase(namespace.getDatabaseName());
try {
database.runCommand(new Document("drop", namespace.getCollectionName())).timeout(10, SECONDS).toBlocking().first();
} catch (MongoCommandException e) {
if (!e.getErrorMessage().startsWith("ns not found")) {
throw e;
}
}
return database.getCollection(namespace.getCollectionName());
}
示例7: dropDatabase
import com.mongodb.MongoCommandException; //导入依赖的package包/类
public static void dropDatabase(final String name) throws Throwable {
if (name == null) {
return;
}
try {
getMongoClient().getDatabase(name).runCommand(new Document("dropDatabase", 1)).timeout(10, SECONDS).toBlocking().first();
} catch (MongoCommandException e) {
if (!e.getErrorMessage().startsWith("ns not found")) {
throw e;
}
}
}
示例8: drop
import com.mongodb.MongoCommandException; //导入依赖的package包/类
public static void drop(final MongoNamespace namespace) throws Throwable {
try {
getMongoClient().getDatabase(namespace.getDatabaseName())
.runCommand(new Document("drop", namespace.getCollectionName())).timeout(10, SECONDS).toBlocking().first();
} catch (MongoCommandException e) {
if (!e.getErrorMessage().contains("ns not found")) {
throw e;
}
}
}
示例9: initializeCollection
import com.mongodb.MongoCommandException; //导入依赖的package包/类
public static MongoCollection<Document> initializeCollection(final MongoNamespace namespace) throws Throwable {
MongoDatabase database = getMongoClient().getDatabase(namespace.getDatabaseName());
try {
RxReactiveStreams.toObservable(database.runCommand(new Document("drop", namespace.getCollectionName())))
.timeout(10, SECONDS).toBlocking().toIterable();
} catch (MongoCommandException e) {
if (!e.getErrorMessage().startsWith("ns not found")) {
throw e;
}
}
return database.getCollection(namespace.getCollectionName());
}
示例10: dropDatabase
import com.mongodb.MongoCommandException; //导入依赖的package包/类
public static void dropDatabase(final String name) throws Throwable {
if (name == null) {
return;
}
try {
RxReactiveStreams.toObservable(getMongoClient().getDatabase(name).runCommand(new Document("dropDatabase", 1)))
.timeout(10, SECONDS).toBlocking().toIterable();
} catch (MongoCommandException e) {
if (!e.getErrorMessage().startsWith("ns not found")) {
throw e;
}
}
}
示例11: drop
import com.mongodb.MongoCommandException; //导入依赖的package包/类
public static void drop(final MongoNamespace namespace) throws Throwable {
try {
RxReactiveStreams.toObservable(getMongoClient().getDatabase(namespace.getDatabaseName())
.runCommand(new Document("drop", namespace.getCollectionName()))).timeout(10, SECONDS).toBlocking().toIterable();
} catch (MongoCommandException e) {
if (!e.getErrorMessage().contains("ns not found")) {
throw e;
}
}
}
示例12: initializeCollection
import com.mongodb.MongoCommandException; //导入依赖的package包/类
public static MongoCollection<Document> initializeCollection(final MongoNamespace namespace) throws Throwable {
MongoDatabase database = getMongoClient().getDatabase(namespace.getDatabaseName());
try {
ObservableSubscriber<Document> subscriber = new ObservableSubscriber<Document>();
database.runCommand(new Document("drop", namespace.getCollectionName())).subscribe(subscriber);
subscriber.await(10, SECONDS);
} catch (MongoCommandException e) {
if (!e.getErrorMessage().startsWith("ns not found")) {
throw e;
}
}
return database.getCollection(namespace.getCollectionName());
}
示例13: dropDatabase
import com.mongodb.MongoCommandException; //导入依赖的package包/类
public static void dropDatabase(final String name) throws Throwable {
if (name == null) {
return;
}
try {
ObservableSubscriber<Document> subscriber = new ObservableSubscriber<Document>();
getMongoClient().getDatabase(name).runCommand(new Document("dropDatabase", 1)).subscribe(subscriber);
subscriber.await(10, SECONDS);
} catch (MongoCommandException e) {
if (!e.getErrorMessage().startsWith("ns not found")) {
throw e;
}
}
}
示例14: drop
import com.mongodb.MongoCommandException; //导入依赖的package包/类
public static void drop(final MongoNamespace namespace) throws Throwable {
try {
ObservableSubscriber<Document> subscriber = new ObservableSubscriber<Document>();
getMongoClient().getDatabase(namespace.getDatabaseName())
.runCommand(new Document("drop", namespace.getCollectionName()))
.subscribe(subscriber);
subscriber.await(20, SECONDS);
} catch (MongoCommandException e) {
if (!e.getErrorMessage().contains("ns not found")) {
throw e;
}
}
}
示例15: assertDuplicateKeyException
import com.mongodb.MongoCommandException; //导入依赖的package包/类
/**
* Ensures current exception has been generated due to a duplicate (primary) key.
* Differentiates between Fongo and Mongo exceptions since the behaviour under these databases
* is different.
*/
public static void assertDuplicateKeyException(Throwable exception) {
Preconditions.checkNotNull(exception, "exception");
// unwrap, if necessary
exception = exception instanceof MongoException ? exception : exception.getCause();
// fongo throws directly DuplicateKeyException
if (exception instanceof DuplicateKeyException) return;
// MongoDB throws custom exception
if (exception instanceof MongoCommandException) {
String codeName = ((MongoCommandException) exception).getResponse().get("codeName").asString().getValue();
int errorCode = ((MongoCommandException) exception).getErrorCode();
check(codeName).is("DuplicateKey");
check(errorCode).is(11000);
// all good here (can return)
return;
}
// for bulk writes as well
if (exception instanceof MongoBulkWriteException) {
List<BulkWriteError> errors = ((MongoBulkWriteException) exception).getWriteErrors();
check(errors).hasSize(1);
check(errors.get(0).getCode()).is(11000);
check(errors.get(0).getMessage()).contains("duplicate key");
return;
}
// if we got here means there is a problem (no duplicate key exception)
fail("Should get duplicate key exception after " + exception);
}