Search in sources :

Example 76 with Document

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

the class MongoCollectionTest method testFindAndUpdateWithGenerics.

@Test
public void testFindAndUpdateWithGenerics() {
    CodecRegistry codecRegistry = fromProviders(asList(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new ConcreteCodecProvider()));
    MongoCollection<Concrete> collection = database.getCollection(getCollectionName()).withDocumentClass(Concrete.class).withCodecRegistry(codecRegistry).withReadPreference(ReadPreference.primary()).withWriteConcern(WriteConcern.ACKNOWLEDGED);
    Concrete doc = new Concrete(new ObjectId(), "str", 5, 10L, 4.0, 3290482390480L);
    collection.insertOne(doc);
    Concrete newDoc = collection.findOneAndUpdate(new Document("i", 5), new Document("$set", new Document("i", 6)));
    assertNotNull(newDoc);
    assertEquals(doc, newDoc);
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) ObjectId(org.bson.types.ObjectId) DocumentCodecProvider(org.bson.codecs.DocumentCodecProvider) Document(org.bson.Document) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 77 with Document

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

the class MongoCollectionTest method testAggregationToACollection.

@Test
public void testAggregationToACollection() {
    assumeTrue(serverVersionAtLeast(2, 6));
    // given
    List<Document> documents = asList(new Document("_id", 1), new Document("_id", 2));
    getCollectionHelper().insertDocuments(new DocumentCodec(), documents);
    MongoCollection<Document> collection = database.getCollection(getCollectionName());
    // when
    List<Document> result = collection.aggregate(Collections.singletonList(new Document("$out", "outCollection"))).into(new ArrayList<Document>());
    // then
    assertEquals(documents, result);
}
Also used : DocumentCodec(org.bson.codecs.DocumentCodec) Document(org.bson.Document) Test(org.junit.Test)

Example 78 with Document

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

the class CommandMonitoringTest method setUp.

@Before
public void setUp() {
    ServerVersion serverVersion = ClusterFixture.getServerVersion();
    if (definition.containsKey("ignore_if_server_version_less_than")) {
        assumeFalse(serverVersion.compareTo(getServerVersion("ignore_if_server_version_less_than")) < 0);
    }
    if (definition.containsKey("ignore_if_server_version_greater_than")) {
        assumeFalse(serverVersion.compareTo(getServerVersion("ignore_if_server_version_greater_than")) > 0);
    }
    if (definition.containsKey("ignore_if_topology_type")) {
        BsonArray topologyTypes = definition.getArray("ignore_if_topology_type");
        for (BsonValue type : topologyTypes) {
            String typeString = type.asString().getValue();
            if (typeString.equals("sharded")) {
                assumeFalse(isSharded());
            } else if (typeString.equals("replica_set")) {
                assumeFalse(isDiscoverableReplicaSet());
            } else if (typeString.equals("standalone")) {
                assumeFalse(isSharded());
            }
        }
    }
    List<BsonDocument> documents = new ArrayList<BsonDocument>();
    for (BsonValue document : data) {
        documents.add(document.asDocument());
    }
    CollectionHelper<Document> collectionHelper = new CollectionHelper<Document>(new DocumentCodec(), new MongoNamespace(databaseName, collectionName));
    collectionHelper.drop();
    collectionHelper.insertDocuments(documents);
    commandListener.reset();
    collection = mongoClient.getDatabase(databaseName).getCollection(collectionName, BsonDocument.class);
    if (definition.getDocument("operation").containsKey("read_preference")) {
        collection = collection.withReadPreference(ReadPreference.valueOf(definition.getDocument("operation").getDocument("read_preference").getString("mode").getValue()));
    }
    helper = new JsonPoweredCrudTestHelper(description, collection);
}
Also used : ServerVersion(com.mongodb.connection.ServerVersion) ArrayList(java.util.ArrayList) DocumentCodec(org.bson.codecs.DocumentCodec) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BsonString(org.bson.BsonString) MongoNamespace(com.mongodb.MongoNamespace) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) CollectionHelper(com.mongodb.client.test.CollectionHelper) BsonValue(org.bson.BsonValue) Before(org.junit.Before)

Example 79 with Document

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

the class FindAndUpdateAcceptanceTest method shouldFindAndReplaceWithDocumentRequiringACustomEncoder.

@Test
public void shouldFindAndReplaceWithDocumentRequiringACustomEncoder() {
    Worker pat = new Worker(new ObjectId(), "Pat", "Sales", new Date(), 7);
    CodecRegistry codecRegistry = fromProviders(asList(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new WorkerCodecProvider()));
    MongoCollection<Worker> collection = database.getCollection(getCollectionName(), Worker.class).withCodecRegistry(codecRegistry);
    collection.insertOne(pat);
    assertThat(collection.count(), is(1L));
    Document updateOperation = new Document("$inc", new Document("numberOfJobs", 1));
    Worker updatedDocument = collection.findOneAndUpdate(new Document("name", "Pat"), updateOperation, new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER));
    assertThat("Worker returned from updateOneAndGet should have the", updatedDocument.getNumberOfJobs(), equalTo(8));
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) ObjectId(org.bson.types.ObjectId) DocumentCodecProvider(org.bson.codecs.DocumentCodecProvider) WorkerCodecProvider(com.mongodb.client.test.WorkerCodecProvider) Worker(com.mongodb.client.test.Worker) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Date(java.util.Date) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 80 with Document

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

the class FindAndUpdateAcceptanceTest method shouldUpdateDocumentAndReturnNew.

@Test
public void shouldUpdateDocumentAndReturnNew() {
    Document documentInserted = new Document(KEY, VALUE_TO_CARE_ABOUT).append("someNumber", 11);
    collection.insertOne(documentInserted);
    assertThat(collection.count(), is(1L));
    Document updateOperation = new Document("$inc", new Document("someNumber", 1));
    Document updatedDocument = collection.findOneAndUpdate(new Document(KEY, VALUE_TO_CARE_ABOUT), updateOperation, new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER));
    assertThat("Document returned from updateOneAndGet should be the updated document", (Integer) updatedDocument.get("someNumber"), equalTo(12));
}
Also used : FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) 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