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
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):
The port and host can also be explicitly specified:
Or use the MongoDB Connection String URI format:
Connecting to a Database
A MongoDB server can host multiple independent databases. List the available databases on the server:
This is equivalent to the function:
Connecting to a Collection
A collection is a collection of documents. Getting a collection is the same as getting a database:
The above syntax is equivalent to:
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:
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:
Get a list of the inserted document IDs:
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:
Inserting Multiple Documents into a Collection
It is more efficient to insert many documents at once into a collection. Create a list of documents:
Insert these two documents into the collection:
Getting a Single Document
The simplest query is performed with MongoCollectionFindOne, which returns a single document from the collection:
We can specify that we want to find a document of a three year old cat:
Various keys that are not wanted can be eliminated using projection. Eliminating the "date" and "sex" fields:
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:
Calling Read (or equivalently MongoCursorNext) on a MongoCursor gets the next document:
Once all documents have been read from the cursor, Null is returned if Read is called again:
To read the documents again, a new cursor needs to be created:
Calling ReadList (or equivalently MongoCursorToArray) gets a list of all remaining documents:
All documents in a cursor can also be read into a Dataset:
The real power of cursors is that every document in a collection can be processed without having to load all documents into memory:
This allows for the handling of massive datasets.
Sampling From a Collection
Collections can be sampled from:
Read the cursor into a Dataset:
Modifying Documents
Modify all documents with "age" greater than 4 so that "age" becomes 10:
Deleting Documents
Warning: deleting documents is dangerous, and cannot easily be undone. Delete all documents in a collection with "age" of 3: