Search in sources :

Example 21 with Document

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

the class CollectionAcceptanceTest method shouldGroupDocumentsWhenUsingAggregate.

@Test
public void shouldGroupDocumentsWhenUsingAggregate() {
    insertAggregationTestDocuments();
    List<Document> grouped = collection.aggregate(asList(new Document("$sort", new Document("_id", 1)), new Document("$project", new Document("_id", 0).append("tags", 1)), new Document("$unwind", "$tags"), new Document("$group", new Document("_id", "$tags")), new Document("$sort", new Document("_id", 1)))).into(new ArrayList<Document>());
    assertEquals(asList(new Document("_id", "CE"), new Document("_id", "SA"), new Document("_id", "driver"), new Document("_id", "kernel")), grouped);
}
Also used : Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.Test)

Example 22 with Document

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

the class CollectionAcceptanceTest method shouldAcceptDocumentsWithAllValidValueTypes.

@Test
public void shouldAcceptDocumentsWithAllValidValueTypes() {
    Document doc = new Document();
    doc.append("_id", new ObjectId());
    doc.append("bool", true);
    doc.append("int", 3);
    doc.append("long", 5L);
    doc.append("str", "Hello MongoDB");
    doc.append("double", 1.1);
    doc.append("date", new Date());
    doc.append("ts", new BsonTimestamp(5, 1));
    doc.append("pattern", new BsonRegularExpression("abc"));
    doc.append("minKey", new MinKey());
    doc.append("maxKey", new MaxKey());
    doc.append("js", new Code("code"));
    doc.append("jsWithScope", new CodeWithScope("code", new Document()));
    doc.append("null", null);
    doc.append("binary", new Binary((byte) 42, new byte[] { 10, 11, 12 }));
    doc.append("list", Arrays.asList(7, 8, 9));
    doc.append("doc list", Arrays.asList(new Document("x", 1), new Document("x", 2)));
    collection.insertOne(doc);
    Document found = collection.find().first();
    assertNotNull(found);
    assertEquals(ObjectId.class, found.get("_id").getClass());
    assertEquals(Boolean.class, found.get("bool").getClass());
    assertEquals(Integer.class, found.get("int").getClass());
    assertEquals(Long.class, found.get("long").getClass());
    assertEquals(String.class, found.get("str").getClass());
    assertEquals(Double.class, found.get("double").getClass());
    assertEquals(Date.class, found.get("date").getClass());
    assertEquals(BsonTimestamp.class, found.get("ts").getClass());
    assertEquals(BsonRegularExpression.class, found.get("pattern").getClass());
    assertEquals(MinKey.class, found.get("minKey").getClass());
    assertEquals(MaxKey.class, found.get("maxKey").getClass());
    assertEquals(Code.class, found.get("js").getClass());
    assertEquals(CodeWithScope.class, found.get("jsWithScope").getClass());
    assertNull(found.get("null"));
    assertEquals(Binary.class, found.get("binary").getClass());
    assertTrue(found.get("list") instanceof List);
    assertTrue(found.get("doc list") instanceof List);
}
Also used : MinKey(org.bson.types.MinKey) ObjectId(org.bson.types.ObjectId) MaxKey(org.bson.types.MaxKey) CodeWithScope(org.bson.types.CodeWithScope) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Binary(org.bson.types.Binary) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonRegularExpression(org.bson.BsonRegularExpression) Code(org.bson.types.Code) Date(java.util.Date) BsonTimestamp(org.bson.BsonTimestamp) Test(org.junit.Test)

Example 23 with Document

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

the class CollectionAcceptanceTest method shouldProjectDocumentsWhenUsingAggregate.

@Test
public void shouldProjectDocumentsWhenUsingAggregate() {
    insertAggregationTestDocuments();
    List<Document> sorted = collection.aggregate(asList(new Document("$sort", new Document("_id", 1)), new Document("$project", new Document("_id", 0).append("zip", "$_id")))).into(new ArrayList<Document>());
    assertEquals(asList(new Document("zip", "01778"), new Document("zip", "10012"), new Document("zip", "94301")), sorted);
}
Also used : Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.Test)

Example 24 with Document

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

the class CollectionAcceptanceTest method shouldBeAbleToRenameCollectionToAnExistingCollectionNameAndReplaceItWhenDropIsTrue.

@Test
public void shouldBeAbleToRenameCollectionToAnExistingCollectionNameAndReplaceItWhenDropIsTrue() {
    //given
    String existingCollectionName = "anExistingCollection";
    String keyInOriginalCollection = "someKey";
    String valueInOriginalCollection = "someValue";
    collection.insertOne(new Document(keyInOriginalCollection, valueInOriginalCollection));
    MongoCollection<Document> existingCollection = database.getCollection(existingCollectionName);
    String keyInExistingCollection = "aDifferentDocument";
    String valueInExistingCollection = "withADifferentValue";
    existingCollection.insertOne(new Document(keyInExistingCollection, valueInExistingCollection));
    assertThat(database.listCollectionNames().into(new ArrayList<String>()).contains(getCollectionName()), is(true));
    assertThat(database.listCollectionNames().into(new ArrayList<String>()).contains(existingCollectionName), is(true));
    //when
    collection.renameCollection(new MongoNamespace(getDatabaseName(), existingCollectionName), new RenameCollectionOptions().dropTarget(true));
    //then
    assertThat(database.listCollectionNames().into(new ArrayList<String>()).contains(getCollectionName()), is(false));
    assertThat(database.listCollectionNames().into(new ArrayList<String>()).contains(existingCollectionName), is(true));
    MongoCollection<Document> replacedCollection = database.getCollection(existingCollectionName);
    assertThat(replacedCollection.find().first().get(keyInExistingCollection), is(nullValue()));
    assertThat(replacedCollection.find().first().get(keyInOriginalCollection).toString(), is(valueInOriginalCollection));
}
Also used : RenameCollectionOptions(com.mongodb.client.model.RenameCollectionOptions) BsonString(org.bson.BsonString) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) MongoNamespace(com.mongodb.MongoNamespace) Test(org.junit.Test)

Example 25 with Document

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

the class CollectionAcceptanceTest method shouldSortDocumentsWhenUsingAggregate.

@Test
public void shouldSortDocumentsWhenUsingAggregate() {
    List<Document> documents = insertAggregationTestDocuments();
    List<Document> sorted = collection.aggregate(asList(new Document("$sort", new Document("_id", 1)))).into(new ArrayList<Document>());
    assertEquals(documents, sorted);
}
Also used : Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) 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