Search in sources :

Example 61 with Document

use of org.bson.Document in project mongo-java-driver by mongodb.

the class Fixture method initializeCollection.

public static MongoCollection<Document> initializeCollection(final MongoNamespace namespace) {
    MongoDatabase database = getMongoClient().getDatabase(namespace.getDatabaseName());
    try {
        FutureResultCallback<Document> futureResultCallback = new FutureResultCallback<Document>();
        database.runCommand(new Document("drop", namespace.getCollectionName()), futureResultCallback);
        futureResultCallback.get(60, SECONDS);
    } catch (MongoCommandException e) {
        if (!e.getErrorMessage().startsWith("ns not found")) {
            throw e;
        }
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
    return database.getCollection(namespace.getCollectionName());
}
Also used : FutureResultCallback(com.mongodb.async.FutureResultCallback) MongoCommandException(com.mongodb.MongoCommandException) Document(org.bson.Document)

Example 62 with Document

use of org.bson.Document in project mongo-java-driver by mongodb.

the class Fixture method dropDatabase.

public static void dropDatabase(final String name) {
    if (name == null) {
        return;
    }
    try {
        FutureResultCallback<Document> futureResultCallback = new FutureResultCallback<Document>();
        getMongoClient().getDatabase(name).runCommand(new Document("dropDatabase", 1), futureResultCallback);
        futureResultCallback.get(60, SECONDS);
    } catch (MongoCommandException e) {
        if (!e.getErrorMessage().startsWith("ns not found")) {
            throw e;
        }
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}
Also used : FutureResultCallback(com.mongodb.async.FutureResultCallback) MongoCommandException(com.mongodb.MongoCommandException) Document(org.bson.Document)

Example 63 with Document

use of org.bson.Document in project mongo-java-driver by mongodb.

the class ClusterFixture method enableMaxTimeFailPoint.

public static void enableMaxTimeFailPoint() {
    assumeThat(isSharded(), is(false));
    new CommandWriteOperation<BsonDocument>("admin", new BsonDocumentWrapper<Document>(new Document("configureFailPoint", "maxTimeAlwaysTimeOut").append("mode", "alwaysOn"), new DocumentCodec()), new BsonDocumentCodec()).execute(getBinding());
}
Also used : BsonDocument(org.bson.BsonDocument) DocumentCodec(org.bson.codecs.DocumentCodec) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BsonDocumentWrapper(org.bson.BsonDocumentWrapper) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 64 with Document

use of org.bson.Document in project mongo-java-driver by mongodb.

the class ClusterFixture method isEnterpriseServer.

@SuppressWarnings("unchecked")
public static boolean isEnterpriseServer() {
    Document buildInfo = getBuildInfo();
    List<String> modules = Collections.emptyList();
    if (buildInfo.containsKey("modules")) {
        modules = (List<String>) buildInfo.get("modules");
    }
    return modules.contains("enterprise");
}
Also used : Document(org.bson.Document) BsonDocument(org.bson.BsonDocument)

Example 65 with Document

use of org.bson.Document in project mongo-java-driver by mongodb.

the class UpdatePrimer method replaceDocument.

@Test
public void replaceDocument() {
    assumeTrue(serverVersionAtLeast(2, 6));
    // @begin: replace-document
    // @code: start
    db.getCollection("restaurants").replaceOne(new Document("restaurant_id", "41704620"), new Document("address", new Document().append("street", "2 Avenue").append("zipcode", "10075").append("building", "1480").append("coord", asList(-73.9557413, 40.7720266))).append("name", "Vella 2"));
// @code: end
/*
       // @post: start
           The replaceOne operation returns a ``UpdateResult`` which contains information about the operation.
           The ``getModifiedCount`` method returns the number of documents modified.
       // @post: end
       */
// @end: replace-document
}
Also used : Document(org.bson.Document) Test(org.junit.Test)

Aggregations

Document (org.bson.Document)1104 Test (org.junit.Test)795 ArrayList (java.util.ArrayList)74 Update (org.springframework.data.mongodb.core.query.Update)71 List (java.util.List)55 BsonDocument (org.bson.BsonDocument)53 ObjectId (org.bson.types.ObjectId)41 MongoDatabase (com.mongodb.client.MongoDatabase)40 Query (org.springframework.data.mongodb.core.query.Query)40 BasicDBObject (com.mongodb.BasicDBObject)39 MongoClient (com.mongodb.MongoClient)32 Bson (org.bson.conversions.Bson)32 ReturnDocument (com.mongodb.client.model.ReturnDocument)31 DBObject (com.mongodb.DBObject)27 DBRef (com.mongodb.DBRef)25 UnknownHostException (java.net.UnknownHostException)25 HashMap (java.util.HashMap)24 FullDocument (com.mongodb.client.model.changestream.FullDocument)23 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)21 MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)21