Search in sources :

Example 31 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 32 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 33 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)

Example 34 with Document

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

the class DropIndexAcceptanceTest method shouldDropSingleNamedIndex.

@Test
public void shouldDropSingleNamedIndex() {
    // Given
    collection.createIndex(new Document("field", 1));
    assertThat("Should be default index and new index on the database now", collection.listIndexes().into(new ArrayList<Document>()).size(), is(2));
    // When
    collection.dropIndex("field_1");
    // Then
    assertThat("Should be one less index", collection.listIndexes().into(new ArrayList<Document>()).size(), is(1));
}
Also used : Document(org.bson.Document) Test(org.junit.Test)

Example 35 with Document

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

the class FilterAcceptanceTest method shouldReturnResultsInTheReverseOrderTheyAreOnDiskWhenNaturalSortOfMinusOneApplied.

@Test
public void shouldReturnResultsInTheReverseOrderTheyAreOnDiskWhenNaturalSortOfMinusOneApplied() {
    // Given
    collection.insertOne(new Document("name", "Chris"));
    collection.insertOne(new Document("name", "Adam"));
    collection.insertOne(new Document("name", "Bob"));
    // When
    MongoCursor<Document> sortedCollection = collection.find().sort(new Document("$natural", -1)).iterator();
    // Then
    assertThat(sortedCollection.next().get("name").toString(), is("Bob"));
    assertThat(sortedCollection.next().get("name").toString(), is("Adam"));
    assertThat(sortedCollection.next().get("name").toString(), is("Chris"));
}
Also used : Document(org.bson.Document) Test(org.junit.Test)

Aggregations

Document (org.bson.Document)2386 Test (org.junit.jupiter.api.Test)900 Test (org.junit.Test)472 ArrayList (java.util.ArrayList)209 BsonDocument (org.bson.BsonDocument)188 List (java.util.List)160 Bson (org.bson.conversions.Bson)133 MongoDatabase (com.mongodb.client.MongoDatabase)119 Update (org.springframework.data.mongodb.core.query.Update)116 ObjectId (org.bson.types.ObjectId)113 Map (java.util.Map)92 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)83 Test (org.testng.annotations.Test)82 HashMap (java.util.HashMap)79 BasicDBObject (com.mongodb.BasicDBObject)75 Query (org.springframework.data.mongodb.core.query.Query)67 MongoCollection (com.mongodb.client.MongoCollection)54 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)50 MongoClient (com.mongodb.MongoClient)48 Date (java.util.Date)47