<commit_before>This tutorial takes you through inserting, updating, and querying documents.

### Getting Ready

Start a local MongoDB server in a separate terminal window:

	$ mkdir mongoFiles
	$ mongod --dbpath mongoFiles

Start a haskell session:

	$ ghci
	> :set prompt "> "

Import the MongoDB driver library, and set OverloadedStrings so literal strings are converted to UTF-8 automatically.

	> :set -XOverloadedStrings
	> import Database.MongoDB

### Connecting

Establish a connection to your local Mongo server on the standard port (27017):

	> pipe <- connect $ host "127.0.0.1"

A host with non-standard port would look like `Host "127.0.0.1" (PortNumber 27001)`.

`connect h` has type `ErrorT IOError IO Pipe`. `runIOE` brings this type back down to `IO Pipe` and throws the IOError in IO if present. One design principle of this driver was to make DB errors explicit, hence the need for `runIOE`.

A `Pipe` is a thread-safe, pipelined (a' la [HTTP pipelining](http://en.wikipedia.org/wiki/HTTP_pipelining)) TCP connection to a MongoDB server. Multiple threads can use the pipe at the same time. The pipelining feature is used by cursors and not exposed to the user.

### Action monad

A DB read or write operation is called a DB `Action`. A DB Action is a monad so you can sequence them together. To run an Action supply it to the `access` function with the Pipe to use, the `AccessMode` for read/write operations, and the `Database` to access. For example, to list all collections in the "test" database:

	> access pipe master "test" allCollections<commit_msg>Adding simple set for BSON to work on ghci but also from scripts<commit_after>This tutorial takes you through inserting, updating, and querying documents.

### Getting Ready

Start a local MongoDB server in a separate terminal window:

	$ mkdir mongoFiles
	$ mongod --dbpath mongoFiles

Start a haskell session:

	$ ghci
	> :set prompt "> "

Import the MongoDB driver library, and set OverloadedStrings so literal strings are converted to UTF-8 automatically and ExtendedDefaultRules for using BSON fields with basic types.

	> :set -XOverloadedStrings
	> :set -XExtendedDefaultRules
	> import Database.MongoDB

### Connecting

Establish a connection to your local Mongo server on the standard port (27017):

	> pipe <- connect $ host "127.0.0.1"

A host with non-standard port would look like `Host "127.0.0.1" (PortNumber 27001)`.

`connect h` has type `ErrorT IOError IO Pipe`. `runIOE` brings this type back down to `IO Pipe` and throws the IOError in IO if present. One design principle of this driver was to make DB errors explicit, hence the need for `runIOE`.

A `Pipe` is a thread-safe, pipelined (a' la [HTTP pipelining](http://en.wikipedia.org/wiki/HTTP_pipelining)) TCP connection to a MongoDB server. Multiple threads can use the pipe at the same time. The pipelining feature is used by cursors and not exposed to the user.

### Action monad

A DB read or write operation is called a DB `Action`. A DB Action is a monad so you can sequence them together. To run an Action supply it to the `access` function with the Pipe to use, the `AccessMode` for read/write operations, and the `Database` to access. For example, to list all collections in the "test" database:

	> access pipe master "test" allCollections
{'fraction_non_alphanumeric': 0.07008495145631068, 'fraction_numerical': 0.013956310679611651, 'mean_word_length': 4.241653418124006, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2796666', 'n_tokens_mistral': 972, 'n_tokens_neox': 914, 'n_words': 482}