Creating documents

In order to create new documents, you can use any of the following methods:

    • MongoDB\Collection::insertOne()
    • MongoDB\Collection::insertMany()
    • MongoDB\Collection::bulkWrite()

Obviously, the last two are convenient when you want to insert multiple documents in one operation. The main downside to the last two methods, as defined in PHPLIB, is that you need to provide an array of documents. This takes up memory and might not be appropriate when a massive amount of data needs to be inserted.

In this example, however, the CSV file only contains 82 entries, and thus insertMany() is used:

// initialize env require __DIR__ . '/vendor/autoload.php'; use Application\ {Client, Csv};  // set up mongodb client + collection $params ...

Get MongoDB 4 Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.