Search in sources :

Example 11 with CreateCollectionOptions

use of com.mongodb.client.model.CreateCollectionOptions in project mongo-java-driver by mongodb.

the class QuickTourAdmin method main.

/**
     * Run this main method to see the output of this quick example.
     *
     * @param args takes an optional single argument for the connection string
     */
public static void main(final String[] args) {
    MongoClient mongoClient;
    if (args.length == 0) {
        // connect to the local database server
        mongoClient = new MongoClient();
    } else {
        mongoClient = new MongoClient(new MongoClientURI(args[0]));
    }
    // get handle to "mydb" database
    MongoDatabase database = mongoClient.getDatabase("mydb");
    database.drop();
    // get a handle to the "test" collection
    MongoCollection<Document> collection = database.getCollection("test");
    // drop all the data in it
    collection.drop();
    // getting a list of databases
    for (String name : mongoClient.listDatabaseNames()) {
        System.out.println(name);
    }
    // drop a database
    mongoClient.getDatabase("databaseToBeDropped").drop();
    // create a collection
    database.createCollection("cappedCollection", new CreateCollectionOptions().capped(true).sizeInBytes(0x100000));
    for (String name : database.listCollectionNames()) {
        System.out.println(name);
    }
    // drop a collection:
    collection.drop();
    // create an ascending index on the "i" field
    collection.createIndex(Indexes.ascending("i"));
    // list the indexes on the collection
    for (final Document index : collection.listIndexes()) {
        System.out.println(index.toJson());
    }
    // create a text index on the "content" field
    collection.createIndex(Indexes.text("content"));
    collection.insertOne(new Document("_id", 0).append("content", "textual content"));
    collection.insertOne(new Document("_id", 1).append("content", "additional content"));
    collection.insertOne(new Document("_id", 2).append("content", "irrelevant content"));
    // Find using the text index
    long matchCount = collection.count(text("textual content -irrelevant"));
    System.out.println("Text search matches: " + matchCount);
    // Find using the $language operator
    Bson textSearch = text("textual content -irrelevant", new TextSearchOptions().language("english"));
    matchCount = collection.count(textSearch);
    System.out.println("Text search matches (english): " + matchCount);
    // Find the highest scoring match
    Document projection = new Document("score", new Document("$meta", "textScore"));
    Document myDoc = collection.find(textSearch).projection(projection).first();
    System.out.println("Highest scoring document: " + myDoc.toJson());
    // Run a command
    Document buildInfo = database.runCommand(new Document("buildInfo", 1));
    System.out.println(buildInfo);
    // release resources
    database.drop();
    mongoClient.close();
}
Also used : MongoClient(com.mongodb.MongoClient) MongoClientURI(com.mongodb.MongoClientURI) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) TextSearchOptions(com.mongodb.client.model.TextSearchOptions) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase) Bson(org.bson.conversions.Bson)

Example 12 with CreateCollectionOptions

use of com.mongodb.client.model.CreateCollectionOptions in project camel by apache.

the class MongoDbTailableCursorConsumerTest method testHundredThousandRecords.

@Test
public void testHundredThousandRecords() throws Exception {
    assertEquals(0, cappedTestCollection.count());
    final MockEndpoint mock = getMockEndpoint("mock:test");
    mock.expectedMessageCount(1000);
    // create a capped collection with max = 1000
    // DocumentBuilder.start().add("capped", true).add("size",
    // 1000000000).add("max", 1000).get())
    db.createCollection(cappedTestCollectionName, new CreateCollectionOptions().capped(true).sizeInBytes(1000000000).maxDocuments(1000));
    cappedTestCollection = db.getCollection(cappedTestCollectionName, Document.class);
    addTestRoutes();
    context.startRoute("tailableCursorConsumer1");
    // continuous pump of 100000 records, asserting incrementally to reduce
    // overhead on the mock endpoint
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            for (int i = 1; i <= 100000; i++) {
                cappedTestCollection.insertOne(new Document("increasing", i).append("string", "value" + i));
                // messages and otherwise the test would be sluggish
                if (i % 1000 == 0) {
                    try {
                        MongoDbTailableCursorConsumerTest.this.assertAndResetMockEndpoint(mock);
                    } catch (Exception e) {
                        return;
                    }
                }
            }
        }
    });
    // start the data pumping
    t.start();
    // before we stop the route, wait for the data pumping to end
    t.join();
    context.stopRoute("tailableCursorConsumer1");
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) Document(org.bson.Document) Test(org.junit.Test)

Example 13 with CreateCollectionOptions

use of com.mongodb.client.model.CreateCollectionOptions in project camel by apache.

the class MongoDbTailableCursorConsumerTest method testPersistentTailTrackIncreasingDateField.

@Test
@Ignore
public void testPersistentTailTrackIncreasingDateField() throws Exception {
    assertEquals(0, cappedTestCollection.count());
    final MockEndpoint mock = getMockEndpoint("mock:test");
    final Calendar startTimestamp = Calendar.getInstance();
    // get default tracking collection
    MongoCollection<Document> trackingCol = db.getCollection(MongoDbTailTrackingConfig.DEFAULT_COLLECTION, Document.class);
    trackingCol.drop();
    trackingCol = db.getCollection(MongoDbTailTrackingConfig.DEFAULT_COLLECTION, Document.class);
    // create a capped collection with max = 1000
    // DocumentBuilder.start().add("capped", true).add("size",
    // 1000000000).add("max", 1000).get()
    db.createCollection(cappedTestCollectionName, new CreateCollectionOptions().capped(true).sizeInBytes(1000000000).maxDocuments(1000));
    cappedTestCollection = db.getCollection(cappedTestCollectionName, Document.class);
    addTestRoutes();
    context.startRoute("tailableCursorConsumer2");
    mock.expectedMessageCount(300);
    // pump 300 records
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            for (int i = 1; i <= 300; i++) {
                Calendar c = (Calendar) (startTimestamp.clone());
                c.add(Calendar.MINUTE, i);
                cappedTestCollection.insertOne(new Document("increasing", c.getTime()).append("string", "value" + i));
            }
        }
    });
    // start the data pumping
    t.start();
    // before we continue wait for the data pump to end
    t.join();
    mock.assertIsSatisfied();
    mock.reset();
    // ensure that the persisted lastVal is startTimestamp + 300min
    Calendar cal300 = (Calendar) startTimestamp.clone();
    cal300.add(Calendar.MINUTE, 300);
    context.stopRoute("tailableCursorConsumer2");
    assertEquals(cal300.getTime(), trackingCol.find(eq("persistentId", "darwin")).first().get(MongoDbTailTrackingConfig.DEFAULT_FIELD));
    context.startRoute("tailableCursorConsumer2");
    // expect 300 messages and not 600
    mock.expectedMessageCount(300);
    // pump 300 records
    t = new Thread(new Runnable() {

        @Override
        public void run() {
            for (int i = 301; i <= 600; i++) {
                Calendar c = (Calendar) (startTimestamp.clone());
                c.add(Calendar.MINUTE, i);
                cappedTestCollection.insertOne(new Document("increasing", c.getTime()).append("string", "value" + i));
            }
        }
    });
    // start the data pumping
    t.start();
    // before we continue wait for the data pump to end
    t.join();
    mock.assertIsSatisfied();
    Object firstBody = mock.getExchanges().get(0).getIn().getBody();
    assertTrue(firstBody instanceof Document);
    Calendar cal301 = Calendar.class.cast(startTimestamp.clone());
    cal301.add(Calendar.MINUTE, 301);
    assertEquals(cal301.getTime(), Document.class.cast(firstBody).get("increasing"));
    // check that the persisted lastVal after stopping the route is
    // startTimestamp + 600min
    context.stopRoute("tailableCursorConsumer2");
    Calendar cal600 = (Calendar) startTimestamp.clone();
    cal600.add(Calendar.MINUTE, 600);
    assertEquals(cal600.getTime(), trackingCol.find(eq("persistentId", "darwin")).first().get(MongoDbTailTrackingConfig.DEFAULT_FIELD));
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Calendar(java.util.Calendar) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) Document(org.bson.Document) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 14 with CreateCollectionOptions

use of com.mongodb.client.model.CreateCollectionOptions in project camel by apache.

the class MongoDbTailableCursorConsumerTest method testCustomTailTrackLocation.

@Test
@Ignore
public void testCustomTailTrackLocation() throws Exception {
    assertEquals(0, cappedTestCollection.count());
    final MockEndpoint mock = getMockEndpoint("mock:test");
    // get the custom tracking collection and drop it
    // (tailTrackDb=einstein&tailTrackCollection=curie&tailTrackField=newton)
    MongoCollection<Document> trackingCol = mongo.getDatabase("einstein").getCollection("curie", Document.class);
    trackingCol.drop();
    trackingCol = mongo.getDatabase("einstein").getCollection("curie", Document.class);
    // create a capped collection with max = 1000
    // DocumentBuilder.start().add("capped", true).add("size",
    // 1000000000).add("max", 1000).get()
    db.createCollection(cappedTestCollectionName, new CreateCollectionOptions().capped(true).sizeInBytes(1000000000).maxDocuments(1000));
    cappedTestCollection = db.getCollection(cappedTestCollectionName, Document.class);
    addTestRoutes();
    context.startRoute("tailableCursorConsumer3");
    mock.expectedMessageCount(300);
    // pump 300 records
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            for (int i = 1; i <= 300; i++) {
                cappedTestCollection.insertOne(new Document("increasing", i).append("string", "value" + i));
            }
        }
    });
    // start the data pumping
    t.start();
    // before we continue wait for the data pump to end
    t.join();
    mock.assertIsSatisfied();
    mock.reset();
    // stop the route to ensure that our lastVal is persisted, and check it
    context.stopRoute("tailableCursorConsumer3");
    // ensure that the persisted lastVal is 300, newton is the name of the
    // trackingField we are using
    assertEquals(300, trackingCol.find(eq("persistentId", "darwin")).first().get("newton"));
    context.startRoute("tailableCursorConsumer3");
    // expect 300 messages and not 600
    mock.expectedMessageCount(300);
    // pump 300 records
    t = new Thread(new Runnable() {

        @Override
        public void run() {
            for (int i = 301; i <= 600; i++) {
                cappedTestCollection.insertOne(new Document("increasing", i).append("string", "value" + i));
            }
        }
    });
    // start the data pumping
    t.start();
    // before we continue wait for the data pump to end
    t.join();
    mock.assertIsSatisfied();
    // check that the first received body contains increasing=301 and not
    // increasing=1, i.e. it's not starting from the top
    Object firstBody = mock.getExchanges().get(0).getIn().getBody();
    assertTrue(firstBody instanceof Document);
    assertEquals(301, (Document.class.cast(firstBody)).get("increasing"));
    // check that the persisted lastVal after stopping the route is 600,
    // newton is the name of the trackingField we are using
    context.stopRoute("tailableCursorConsumer3");
    assertEquals(600, trackingCol.find(eq("persistentId", "darwin")).first().get("newton"));
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) Document(org.bson.Document) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 15 with CreateCollectionOptions

use of com.mongodb.client.model.CreateCollectionOptions in project camel by apache.

the class MongoDbTailableCursorConsumerTest method testPersistentTailTrack.

@Test
@Ignore
public void testPersistentTailTrack() throws Exception {
    assertEquals(0, cappedTestCollection.count());
    final MockEndpoint mock = getMockEndpoint("mock:test");
    // drop the tracking collection
    db.getCollection(MongoDbTailTrackingConfig.DEFAULT_COLLECTION).drop();
    // create a capped collection with max = 1000
    // DocumentBuilder.start().add("capped", true).add("size",
    // 1000000000).add("max", 1000).get()
    db.createCollection(cappedTestCollectionName, new CreateCollectionOptions().capped(true).sizeInBytes(1000000000).maxDocuments(1000));
    cappedTestCollection = db.getCollection(cappedTestCollectionName, Document.class);
    cappedTestCollection.createIndex(new Document("increasing", 1));
    addTestRoutes();
    context.startRoute("tailableCursorConsumer2");
    mock.expectedMessageCount(300);
    // pump 300 records
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            for (int i = 1; i <= 300; i++) {
                cappedTestCollection.insertOne(new Document("increasing", i).append("string", "value" + i));
            }
        }
    });
    // start the data pumping
    t.start();
    // before we continue wait for the data pump to end
    t.join();
    mock.assertIsSatisfied();
    mock.reset();
    context.stopRoute("tailableCursorConsumer2");
    while (context.getRouteStatus("tailableCursorConsumer2") != ServiceStatus.Stopped) {
    }
    context.startRoute("tailableCursorConsumer2");
    // expect 300 messages and not 600
    mock.expectedMessageCount(300);
    // pump 300 records
    t = new Thread(new Runnable() {

        @Override
        public void run() {
            for (int i = 301; i <= 600; i++) {
                cappedTestCollection.insertOne(new Document("increasing", i).append("string", "value" + i));
            }
        }
    });
    // start the data pumping
    t.start();
    // before we continue wait for the data pump to end
    t.join();
    mock.assertIsSatisfied();
    // check that the first message received in this second batch
    // corresponds to increasing=301
    Object firstBody = mock.getExchanges().get(0).getIn().getBody();
    assertTrue(firstBody instanceof Document);
    assertEquals(301, Document.class.cast(firstBody).get("increasing"));
    // check that the lastVal is persisted at the right time: check before
    // and after stopping the route
    assertEquals(300, db.getCollection(MongoDbTailTrackingConfig.DEFAULT_COLLECTION).find(eq("persistentId", "darwin")).first().get("lastTrackingValue"));
    // stop the route and verify the last value has been updated
    context.stopRoute("tailableCursorConsumer2");
    while (context.getRouteStatus("tailableCursorConsumer2") != ServiceStatus.Stopped) {
    }
    assertEquals(600, db.getCollection(MongoDbTailTrackingConfig.DEFAULT_COLLECTION).find(eq("persistentId", "darwin")).first().get("lastTrackingValue"));
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) Document(org.bson.Document) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)22 Test (org.junit.Test)17 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)14 Document (org.bson.Document)12 BasicDBObject (com.mongodb.BasicDBObject)8 MongoDatabase (com.mongodb.client.MongoDatabase)4 ValidationOptions (com.mongodb.client.model.ValidationOptions)4 DBObject (com.mongodb.DBObject)3 Ignore (org.junit.Ignore)3 MongoCommandException (com.mongodb.MongoCommandException)2 Calendar (java.util.Calendar)2 CommandResult (com.mongodb.CommandResult)1 MongoClient (com.mongodb.MongoClient)1 MongoClientURI (com.mongodb.MongoClientURI)1 TextSearchOptions (com.mongodb.client.model.TextSearchOptions)1 Date (java.util.Date)1 Bson (org.bson.conversions.Bson)1 Book (org.mongodb.morphia.aggregation.AggregationTest.Book)1 CountResult (org.mongodb.morphia.aggregation.AggregationTest.CountResult)1