Search in sources :

Example 1 with InsertManyOptions

use of com.mongodb.client.model.InsertManyOptions in project storm by apache.

the class MongoDBClient method insert.

/**
     * Inserts one or more documents.
     * This method is equivalent to a call to the bulkWrite method.
     * The documents will be inserted in the order provided, 
     * stopping on the first failed insertion. 
     * 
     * @param documents
     */
public void insert(List<Document> documents, boolean ordered) {
    InsertManyOptions options = new InsertManyOptions();
    if (!ordered) {
        options.ordered(false);
    }
    collection.insertMany(documents, options);
}
Also used : InsertManyOptions(com.mongodb.client.model.InsertManyOptions)

Example 2 with InsertManyOptions

use of com.mongodb.client.model.InsertManyOptions in project zeppelin by apache.

the class MongoNotebookRepo method insertFileSystemNotes.

/**
   * If environment variable ZEPPELIN_NOTEBOOK_MONGO_AUTOIMPORT is true,
   * this method will insert local notes into MongoDB on startup.
   * If a note already exists in MongoDB, skip it.
   */
private void insertFileSystemNotes() throws IOException {
    // docs to be imported
    LinkedList<Document> docs = new LinkedList<>();
    NotebookRepo vfsRepo = new VFSNotebookRepo(this.conf);
    List<NoteInfo> infos = vfsRepo.list(null);
    // collect notes to be imported
    for (NoteInfo info : infos) {
        Note note = vfsRepo.get(info.getId(), null);
        Document doc = noteToDocument(note);
        docs.add(doc);
    }
    /*
     * 'ordered(false)' option allows to proceed bulk inserting even though
     * there are duplicated documents. The duplicated documents will be skipped
     * and print a WARN log.
     */
    try {
        coll.insertMany(docs, new InsertManyOptions().ordered(false));
    } catch (MongoBulkWriteException e) {
        //print duplicated document warning log
        printDuplicatedException(e);
    }
    // it does nothing for now but maybe in the future...
    vfsRepo.close();
}
Also used : NoteInfo(org.apache.zeppelin.notebook.NoteInfo) Note(org.apache.zeppelin.notebook.Note) MongoBulkWriteException(com.mongodb.MongoBulkWriteException) Document(org.bson.Document) InsertManyOptions(com.mongodb.client.model.InsertManyOptions)

Example 3 with InsertManyOptions

use of com.mongodb.client.model.InsertManyOptions in project presto by prestodb.

the class MongoPageSink method appendPage.

@Override
public CompletableFuture<?> appendPage(Page page) {
    MongoCollection<Document> collection = mongoSession.getCollection(schemaTableName);
    List<Document> batch = new ArrayList<>(page.getPositionCount());
    for (int position = 0; position < page.getPositionCount(); position++) {
        Document doc = new Document();
        for (int channel = 0; channel < page.getChannelCount(); channel++) {
            MongoColumnHandle column = columns.get(channel);
            doc.append(column.getName(), getObjectValue(columns.get(channel).getType(), page.getBlock(channel), position));
        }
        batch.add(doc);
    }
    collection.insertMany(batch, new InsertManyOptions().ordered(true));
    return NOT_BLOCKED;
}
Also used : ArrayList(java.util.ArrayList) Document(org.bson.Document) InsertManyOptions(com.mongodb.client.model.InsertManyOptions)

Example 4 with InsertManyOptions

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

the class JsonPoweredCrudTestHelper method getInsertManyResult.

BsonDocument getInsertManyResult(final BsonDocument arguments) {
    List<BsonDocument> documents = new ArrayList<BsonDocument>();
    for (BsonValue document : arguments.getArray("documents")) {
        documents.add(document.asDocument());
    }
    collection.insertMany(documents, new InsertManyOptions().ordered(arguments.getBoolean("ordered", BsonBoolean.TRUE).getValue()));
    BsonArray insertedIds = new BsonArray();
    for (BsonDocument document : documents) {
        insertedIds.add(document.get("_id"));
    }
    return toResult(new BsonDocument("insertedIds", insertedIds));
}
Also used : BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) ArrayList(java.util.ArrayList) BsonValue(org.bson.BsonValue) InsertManyOptions(com.mongodb.client.model.InsertManyOptions)

Aggregations

InsertManyOptions (com.mongodb.client.model.InsertManyOptions)4 ArrayList (java.util.ArrayList)2 Document (org.bson.Document)2 MongoBulkWriteException (com.mongodb.MongoBulkWriteException)1 Note (org.apache.zeppelin.notebook.Note)1 NoteInfo (org.apache.zeppelin.notebook.NoteInfo)1 BsonArray (org.bson.BsonArray)1 BsonDocument (org.bson.BsonDocument)1 BsonValue (org.bson.BsonValue)1