Search in sources :

Example 6 with InsertManyResult

use of com.mongodb.client.result.InsertManyResult in project mongo-java-driver by mongodb.

the class QuickTour 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 = MongoClients.create();
    } else {
        mongoClient = MongoClients.create(args[0]);
    }
    // get handle to "mydb" database
    MongoDatabase database = mongoClient.getDatabase("mydb");
    // get a handle to the "test" collection
    final MongoCollection<Document> collection = database.getCollection("test");
    // drop all the data in it
    ObservableSubscriber<Void> successSubscriber = new OperationSubscriber<>();
    collection.drop().subscribe(successSubscriber);
    successSubscriber.await();
    // make a document and insert it
    Document doc = new Document("name", "MongoDB").append("type", "database").append("count", 1).append("info", new Document("x", 203).append("y", 102));
    ObservableSubscriber<InsertOneResult> insertOneSubscriber = new OperationSubscriber<>();
    collection.insertOne(doc).subscribe(insertOneSubscriber);
    insertOneSubscriber.await();
    // get it (since it's the only one in there since we dropped the rest earlier on)
    ObservableSubscriber<Document> documentSubscriber = new PrintDocumentSubscriber();
    collection.find().first().subscribe(documentSubscriber);
    documentSubscriber.await();
    // now, lets add lots of little documents to the collection so we can explore queries and cursors
    List<Document> documents = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        documents.add(new Document("i", i));
    }
    ObservableSubscriber<InsertManyResult> insertManySubscriber = new OperationSubscriber<>();
    collection.insertMany(documents).subscribe(insertManySubscriber);
    insertManySubscriber.await();
    // find first
    documentSubscriber = new PrintDocumentSubscriber();
    collection.find().first().subscribe(documentSubscriber);
    documentSubscriber.await();
    // lets get all the documents in the collection and print them out
    documentSubscriber = new PrintDocumentSubscriber();
    collection.find().subscribe(documentSubscriber);
    documentSubscriber.await();
    // Query Filters
    // now use a query to get 1 document out
    documentSubscriber = new PrintDocumentSubscriber();
    collection.find(eq("i", 71)).first().subscribe(documentSubscriber);
    documentSubscriber.await();
    // now use a range query to get a larger subset
    documentSubscriber = new PrintDocumentSubscriber();
    collection.find(gt("i", 50)).subscribe(documentSubscriber);
    successSubscriber.await();
    // range query with multiple constraints
    documentSubscriber = new PrintDocumentSubscriber();
    collection.find(and(gt("i", 50), lte("i", 100))).subscribe(documentSubscriber);
    successSubscriber.await();
    // Sorting
    documentSubscriber = new PrintDocumentSubscriber();
    collection.find(exists("i")).sort(descending("i")).first().subscribe(documentSubscriber);
    documentSubscriber.await();
    // Projection
    documentSubscriber = new PrintDocumentSubscriber();
    collection.find().projection(excludeId()).first().subscribe(documentSubscriber);
    documentSubscriber.await();
    // Aggregation
    documentSubscriber = new PrintDocumentSubscriber();
    collection.aggregate(asList(match(gt("i", 0)), project(Document.parse("{ITimes10: {$multiply: ['$i', 10]}}")))).subscribe(documentSubscriber);
    documentSubscriber.await();
    documentSubscriber = new PrintDocumentSubscriber();
    collection.aggregate(singletonList(group(null, sum("total", "$i")))).first().subscribe(documentSubscriber);
    documentSubscriber.await();
    // Update One
    ObservableSubscriber<UpdateResult> updateSubscriber = new OperationSubscriber<>();
    collection.updateOne(eq("i", 10), set("i", 110)).subscribe(updateSubscriber);
    updateSubscriber.await();
    // Update Many
    updateSubscriber = new OperationSubscriber<>();
    collection.updateMany(lt("i", 100), inc("i", 100)).subscribe(updateSubscriber);
    updateSubscriber.await();
    // Delete One
    ObservableSubscriber<DeleteResult> deleteSubscriber = new OperationSubscriber<>();
    collection.deleteOne(eq("i", 110)).subscribe(deleteSubscriber);
    deleteSubscriber.await();
    // Delete Many
    deleteSubscriber = new OperationSubscriber<>();
    collection.deleteMany(gte("i", 100)).subscribe(deleteSubscriber);
    deleteSubscriber.await();
    // Create Index
    OperationSubscriber<String> createIndexSubscriber = new PrintSubscriber<>("Create Index Result: %s");
    collection.createIndex(new Document("i", 1)).subscribe(createIndexSubscriber);
    createIndexSubscriber.await();
    // Clean up
    successSubscriber = new OperationSubscriber<>();
    collection.drop().subscribe(successSubscriber);
    successSubscriber.await();
    // release resources
    mongoClient.close();
}
Also used : PrintDocumentSubscriber(reactivestreams.helpers.SubscriberHelpers.PrintDocumentSubscriber) OperationSubscriber(reactivestreams.helpers.SubscriberHelpers.OperationSubscriber) ArrayList(java.util.ArrayList) Document(org.bson.Document) InsertManyResult(com.mongodb.client.result.InsertManyResult) MongoClient(com.mongodb.reactivestreams.client.MongoClient) PrintSubscriber(reactivestreams.helpers.SubscriberHelpers.PrintSubscriber) InsertOneResult(com.mongodb.client.result.InsertOneResult) UpdateResult(com.mongodb.client.result.UpdateResult) DeleteResult(com.mongodb.client.result.DeleteResult) MongoDatabase(com.mongodb.reactivestreams.client.MongoDatabase)

Example 7 with InsertManyResult

use of com.mongodb.client.result.InsertManyResult in project mongo-java-driver by mongodb.

the class DocumentationSamples method testQueryingAtTheTopLevel.

@Test
public void testQueryingAtTheTopLevel() {
    // Start Example 6
    ObservableSubscriber<InsertManyResult> insertManySubscriber = new OperationSubscriber<>();
    collection.insertMany(asList(Document.parse("{ item: 'journal', qty: 25, size: { h: 14, w: 21, uom: 'cm' }, status: 'A' }"), Document.parse("{ item: 'notebook', qty: 50, size: { h: 8.5, w: 11, uom: 'in' }, status: 'A' }"), Document.parse("{ item: 'paper', qty: 100, size: { h: 8.5, w: 11, uom: 'in' }, status: 'D' }"), Document.parse("{ item: 'planner', qty: 75, size: { h: 22.85, w: 30, uom: 'cm' }, status: 'D' }"), Document.parse("{ item: 'postcard', qty: 45, size: { h: 10, w: 15.25, uom: 'cm' }, status: 'A' }"))).subscribe(insertManySubscriber);
    insertManySubscriber.await();
    // End Example 6
    ObservableSubscriber<Long> countSubscriber = new OperationSubscriber<>();
    collection.countDocuments().subscribe(countSubscriber);
    countSubscriber.await();
    // Start Example 7
    FindPublisher<Document> findPublisher = collection.find(new Document());
    // End Example 7
    ObservableSubscriber<Document> findSubscriber = new OperationSubscriber<>();
    findPublisher.subscribe(findSubscriber);
    findSubscriber.await();
    // Start Example 8
    findPublisher = collection.find();
    // End Example 8
    findSubscriber = new OperationSubscriber<>();
    findPublisher.subscribe(findSubscriber);
    findSubscriber.await();
    // Start Example 9
    findPublisher = collection.find(eq("status", "D"));
    // End Example 9
    findSubscriber = new OperationSubscriber<>();
    findPublisher.subscribe(findSubscriber);
    findSubscriber.await();
    // Start Example 10
    findPublisher = collection.find(in("status", "A", "D"));
    // End Example 10
    findSubscriber = new OperationSubscriber<>();
    findPublisher.subscribe(findSubscriber);
    findSubscriber.await();
    // Start Example 11
    findPublisher = collection.find(and(eq("status", "A"), lt("qty", 30)));
    // End Example 11
    findSubscriber = new OperationSubscriber<>();
    findPublisher.subscribe(findSubscriber);
    findSubscriber.await();
    // Start Example 12
    findPublisher = collection.find(or(eq("status", "A"), lt("qty", 30)));
    // End Example 12
    findSubscriber = new OperationSubscriber<>();
    findPublisher.subscribe(findSubscriber);
    findSubscriber.await();
    // Start Example 13
    findPublisher = collection.find(and(eq("status", "A"), or(lt("qty", 30), regex("item", "^p"))));
    // End Example 13
    findSubscriber = new OperationSubscriber<>();
    findPublisher.subscribe(findSubscriber);
    findSubscriber.await();
}
Also used : OperationSubscriber(reactivestreams.helpers.SubscriberHelpers.OperationSubscriber) Document(org.bson.Document) InsertManyResult(com.mongodb.client.result.InsertManyResult) Test(org.junit.Test)

Example 8 with InsertManyResult

use of com.mongodb.client.result.InsertManyResult in project mongo-java-driver by mongodb.

the class DocumentationSamples method testInsert.

@Test
public void testInsert() {
    // Start Example 1
    Document canvas = new Document("item", "canvas").append("qty", 100).append("tags", singletonList("cotton"));
    Document size = new Document("h", 28).append("w", 35.5).append("uom", "cm");
    canvas.put("size", size);
    ObservableSubscriber<InsertOneResult> insertOneSubscriber = new OperationSubscriber<>();
    collection.insertOne(canvas).subscribe(insertOneSubscriber);
    insertOneSubscriber.await();
    // End Example 1
    // Start Example 2
    FindPublisher<Document> findPublisher = collection.find(eq("item", "canvas"));
    // End Example 2
    ObservableSubscriber<Document> findSubscriber = new OperationSubscriber<>();
    findPublisher.subscribe(findSubscriber);
    findSubscriber.await();
    // Start Example 3
    Document journal = new Document("item", "journal").append("qty", 25).append("tags", asList("blank", "red"));
    Document journalSize = new Document("h", 14).append("w", 21).append("uom", "cm");
    journal.put("size", journalSize);
    Document mat = new Document("item", "mat").append("qty", 85).append("tags", singletonList("gray"));
    Document matSize = new Document("h", 27.9).append("w", 35.5).append("uom", "cm");
    mat.put("size", matSize);
    Document mousePad = new Document("item", "mousePad").append("qty", 25).append("tags", asList("gel", "blue"));
    Document mousePadSize = new Document("h", 19).append("w", 22.85).append("uom", "cm");
    mousePad.put("size", mousePadSize);
    ObservableSubscriber<InsertManyResult> insertManySubscriber = new OperationSubscriber<>();
    collection.insertMany(asList(journal, mat, mousePad)).subscribe(insertManySubscriber);
    insertManySubscriber.await();
    // End Example 3
    ObservableSubscriber<Long> countSubscriber = new OperationSubscriber<>();
    collection.countDocuments().subscribe(countSubscriber);
    countSubscriber.await();
}
Also used : OperationSubscriber(reactivestreams.helpers.SubscriberHelpers.OperationSubscriber) Document(org.bson.Document) InsertOneResult(com.mongodb.client.result.InsertOneResult) InsertManyResult(com.mongodb.client.result.InsertManyResult) Test(org.junit.Test)

Example 9 with InsertManyResult

use of com.mongodb.client.result.InsertManyResult in project morphia by mongodb.

the class FiltersTest method testSampleRate.

@Test
public void testSampleRate() {
    checkMinServerVersion(Version.valueOf("4.4.3"));
    int count = 100;
    List<Document> list = new ArrayList<>();
    for (int i = 0; i < count; i++) {
        list.add(new Document("_id", i).append("r", 0));
    }
    String collectionName = "sampleRate";
    InsertManyResult bulk = getDatabase().getCollection(collectionName).insertMany(list, new InsertManyOptions().ordered(false));
    assertEquals(bulk.getInsertedIds().size(), count);
    Document matches = getDs().aggregate(collectionName).match(Miscellaneous.sampleRate(0.33)).count("numMatches").execute(Document.class).next();
    assertTrue(matches.getInteger("numMatches") < 100);
}
Also used : ArrayList(java.util.ArrayList) Document(org.bson.Document) InsertManyResult(com.mongodb.client.result.InsertManyResult) InsertManyOptions(com.mongodb.client.model.InsertManyOptions) Test(org.testng.annotations.Test)

Aggregations

InsertManyResult (com.mongodb.client.result.InsertManyResult)9 Document (org.bson.Document)6 InsertManyOptions (com.mongodb.client.model.InsertManyOptions)4 ArrayList (java.util.ArrayList)4 OperationSubscriber (reactivestreams.helpers.SubscriberHelpers.OperationSubscriber)4 InsertOneResult (com.mongodb.client.result.InsertOneResult)3 Test (org.junit.Test)3 DeleteResult (com.mongodb.client.result.DeleteResult)2 UpdateResult (com.mongodb.client.result.UpdateResult)2 MongoClient (com.mongodb.reactivestreams.client.MongoClient)2 MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)2 BsonDocument (org.bson.BsonDocument)2 BsonValue (org.bson.BsonValue)2 Test (org.testng.annotations.Test)2 ClusterFixture.isServerlessTest (com.mongodb.ClusterFixture.isServerlessTest)1 MongoBulkWriteException (com.mongodb.MongoBulkWriteException)1 InsertOneModel (com.mongodb.client.model.InsertOneModel)1 HashMap (java.util.HashMap)1 List (java.util.List)1 RawBsonDocument (org.bson.RawBsonDocument)1