Search in sources :

Example 91 with Document

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

the class FindAndReplaceAcceptanceTest method shouldReplaceAndReturnNewItemWithDocumentRequiringACustomEncoder.

@Test
public void shouldReplaceAndReturnNewItemWithDocumentRequiringACustomEncoder() {
    Worker pat = new Worker(new ObjectId(), "Pat", "Sales", new Date(), 3);
    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));
    Worker jordan = new Worker(pat.getId(), "Jordan", "Engineer", new Date(), 7);
    Worker returnedDocument = collection.findOneAndReplace(new Document("name", "Pat"), jordan, new FindOneAndReplaceOptions().returnDocument(ReturnDocument.AFTER));
    assertThat("Worker retrieved from replaceOneAndGet should match the updated Worker", returnedDocument, equalTo(jordan));
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) ObjectId(org.bson.types.ObjectId) DocumentCodecProvider(org.bson.codecs.DocumentCodecProvider) WorkerCodecProvider(com.mongodb.client.test.WorkerCodecProvider) Worker(com.mongodb.client.test.Worker) 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 92 with Document

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

the class FindAndReplaceAcceptanceTest method shouldInsertDocumentWhenFilterDoesNotMatchAnyDocumentsAndUpsertFlagIsSet.

@Test
public void shouldInsertDocumentWhenFilterDoesNotMatchAnyDocumentsAndUpsertFlagIsSet() {
    Document originalDocument = new Document(KEY, VALUE_TO_CARE_ABOUT);
    collection.insertOne(originalDocument);
    assertThat(collection.count(), is(1L));
    Document replacementDocument = new Document("_id", new ObjectId()).append("foo", "bar");
    Document document = collection.findOneAndReplace(new Document(KEY, "valueThatDoesNotMatch"), replacementDocument, new FindOneAndReplaceOptions().upsert(true).returnDocument(ReturnDocument.AFTER));
    assertThat(collection.count(), is(2L));
    assertThat("Document retrieved from replaceOneAndGet with filter that doesn't match should match the replacement document", document, equalTo(replacementDocument));
}
Also used : FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) ObjectId(org.bson.types.ObjectId) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) Test(org.junit.Test)

Example 93 with Document

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

the class AddIndexAcceptanceTest method shouldCreateASparseIndex.

@Test
public void shouldCreateASparseIndex() {
    collection.createIndex(new Document("theField", 1), new IndexOptions().sparse(true));
    Boolean sparse = collection.listIndexes().into(new ArrayList<Document>()).get(1).getBoolean("sparse");
    assertThat("Should be a sparse index", sparse, is(true));
}
Also used : IndexOptions(com.mongodb.client.model.IndexOptions) Document(org.bson.Document) Test(org.junit.Test)

Example 94 with Document

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

the class AddIndexAcceptanceTest method shouldSupportCompoundIndexesWithDifferentOrders.

@Test
public void shouldSupportCompoundIndexesWithDifferentOrders() {
    collection.createIndex(new Document("theFirstField", 1).append("theSecondField", -1));
    Document newIndexDetails = collection.listIndexes().into(new ArrayList<Document>()).get(1);
    Document keys = (Document) newIndexDetails.get("key");
    OrderBy orderBy = fromInt((Integer) keys.get("theFirstField"));
    assertThat("First index should be ascending", orderBy, is(ASC));
    orderBy = fromInt((Integer) keys.get("theSecondField"));
    assertThat("Second index should be descending", orderBy, is(DESC));
    assertThat("Index name should contain both field names", (String) newIndexDetails.get("name"), is("theFirstField_1_theSecondField_-1"));
}
Also used : OrderBy(com.mongodb.operation.OrderBy) ArrayList(java.util.ArrayList) Document(org.bson.Document) Test(org.junit.Test)

Example 95 with Document

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

the class DropIndexAcceptanceTest method shouldDropAllIndexesForCollection.

@Test
public void shouldDropAllIndexesForCollection() {
    // Given
    collection.createIndex(new Document("field", 1));
    collection.createIndex(new Document("anotherField", 1));
    assertThat("Should be three indexes on the collection now", collection.listIndexes().into(new ArrayList<Document>()).size(), is(3));
    // When
    collection.dropIndexes();
    // Then
    assertThat("Should only be the default index on the collection", collection.listIndexes().into(new ArrayList<Document>()).size(), is(1));
}
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