MongoLink Introduction
MongoLink is a set of tools for working with MongoDB. This tutorial shows how to perform the most common MongoDB operations using MongoLink.
This tutorial assumes that a MongoDB server is running on your local machine at the default host and port. For platform-dependent instructions for running a MongoDB server locally, see this.
Making a Local Connection

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-m80bby
Create a client connection using the default host "localhost" and port 27017 (this is the default hostname and port when running the MongoDB server on your local machine):

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-8bxqkz

The port and host can also be explicitly specified:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-uwlulg

Or use the MongoDB Connection String URI format:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-biyx3g

Connecting to a Database
A MongoDB server can host multiple independent databases. List the available databases on the server:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-4noh3a


https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-0akmtj

This is equivalent to the function:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-6citby

Connecting to a Collection
A collection is a collection of documents. Getting a collection is the same as getting a database:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-3ndcon

The above syntax is equivalent to:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-roossq

Note: databases and collections are created lazily, so getting a collection or database does not perform any operations on the MongoDB server. They are only created once the first document is inserted into them.
Documents
A Document in MongoDB can be viewed as a (possibly) nested Association whose keys must be strings and whose values are limited to a small set of types (for example, strings, lists, integers, dates, etc). One simple example of a document:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-5sfyj5

For a list available types that can be used as values, see the documentation for the "BSON" format.
Inserting a Single Document into a Collection
Insert the previous document into the "WolframTest" collection:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-j6j48x

Get a list of the inserted document IDs:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-1d8q6i

Note: Every MongoDB document must have an "_id" key. If this key is missing from the document being inserted, it is automatically added to the document with value of type BSONObjectID.
A BSONObjectID object contains various metadata related to its creation:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-s0pvkn

Inserting Multiple Documents into a Collection
It is more efficient to insert many documents at once into a collection. Create a list of documents:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-34vglk

Insert these two documents into the collection:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-zb8wnz

Getting a Single Document
The simplest query is performed with MongoCollectionFindOne, which returns a single document from the collection:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-ijy8lx

We can specify that we want to find a document of a three year old cat:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-lcsola

Various keys that are not wanted can be eliminated using projection. Eliminating the "date" and "sex" fields:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-lpwj6m

This can be used to speed up the transfer of documents from the server, as unwanted parts of a document need not be transferred.
Note: for more information on building queries, the tutorial "Query Documents" from the MongoDB documentation might be useful.
Getting Multiple Documents
MongoCollectionFind has the same syntax as MongoCollectionFindOne, but instead of returning a document, it returns a MongoCursor, appropriate for handling large datasets:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-rzkw0q

Calling Read (or equivalently MongoCursorNext) on a MongoCursor gets the next document:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-w93y5c


https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-ibmxui


https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-rkn8im

Once all documents have been read from the cursor, Null is returned if Read is called again:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-ialhq1
To read the documents again, a new cursor needs to be created:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-3fzplo

Calling ReadList (or equivalently MongoCursorToArray) gets a list of all remaining documents:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-8xynu4

All documents in a cursor can also be read into a Dataset:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-xg06ll

The real power of cursors is that every document in a collection can be processed without having to load all documents into memory:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-5u520b
This allows for the handling of massive datasets.
Sampling From a Collection
Collections can be sampled from:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-3hntes

Read the cursor into a Dataset:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-wxuwfj

Modifying Documents
Modify all documents with "age" greater than 4 so that "age" becomes 10:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-loelba


https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-zni05k

Deleting Documents
Warning: deleting documents is dangerous, and cannot easily be undone. Delete all documents in a collection with "age" of 3:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-horuu1

Delete all documents in a collection:

https://wolfram.com/xid/0bciup9ss2rgcyfpthi01u1ci-j5y5z9
