Search code examples
javamongodbmongo-java-driver

Java MongoDB bulk insert from stream


I'm trying to insert a few million documents into MongoDB in one operation. Using MongoCollection.insertMany does not work for me because it takes a List as input and my data does not fit in memory.

Is it possible to insert documents from a stream instead?


Solution

  • you need to use Bulk Insert operations from your stream. please read: Bulk Insert

    try to execute() the bulk after a reasonable number of documents (inside your consumer lambda) because after all there is a limit to the size of the request Mongo can handle.