# MongoDB Read

### MongoDBRead <a href="#mongodbread" id="mongodbread"></a>

MongoDBRead performs read operations on a Mongo database.

Mongo is a [NoSql database](https://en.wikipedia.org/wiki/NoSQL), which provides a mechanism for storage and retrieval of data that is different to the tabular relations used in relational databases like SQL Server, MySql and Oracle.

### Properties <a href="#properties" id="properties"></a>

{% hint style="danger" %}
Important: For large datasets select 'Row by row' for the _Return options_ property.
{% endhint %}

{% hint style="info" %}
Tip: Select the _Operation_ first, then select the _Return option_ - these selections will determine which other properties will be available to complete.
{% endhint %}

#### Collection <a href="#collection" id="collection"></a>

The collection to read from.

#### Connection string <a href="#connection-string" id="connection-string"></a>

A connection string to your database. [learn more](http://docs.mongodb.org/manual/reference/connection-string/)

#### Operation <a href="#operation" id="operation"></a>

Options:

* _Find_: Selects documents in a collection.
* _Aggregate_: Calculates aggregate values for the data in a collection.

#### Query <a href="#query" id="query"></a>

A MongoDb query document. [learn more](http://docs.mongodb.org/manual/tutorial/query-documents/)

For example, if we select from a collection of documents using the attributes "quantity" and "price", the criteria will return all documents in the collection with fields "quantity" greater than 100 or "price" less than 9.95:

`{ $or: [ { quantity: { $gt: 100 } }, { price: { $lt: 9.95 } } ] }`

#### Fields <a href="#fields" id="fields"></a>

Specifies which fields to return.

`{ field1: value, field2: value ... }`

Format is, as in the example above:

* field = the field name
* value = 1 to include and 0 to exclude

The parameter contains either _include_ or _exclude_ specifications, not both, unless the exclude is for the _\_id_ field.

#### Sort <a href="#sort" id="sort"></a>

Specifies the order in which the query returns matching documents.

`{ field1: value, field2: value ... }`

Format is, as in the example above:

* field = the field name
* value = 1 to sort ascending and -1 to sort descending

#### Skip <a href="#skip" id="skip"></a>

An integer indicating how many documents to skip before returning results.

#### Limit <a href="#limit" id="limit"></a>

An integer indicating the maximum number of documents to return.

#### Pipelines <a href="#pipelines" id="pipelines"></a>

Only available when _Operation_ is 'Aggregate'.

The aggregation pipelines should be specified as a JSON formatted array of aggregation objects.

If we have a collection of "People" documents with attributes "age" and "name", as in the example below, the aggregation pipeline will sort the documents in the collection by age, and group the results into a single document with attributes "total" (containing the total number of records in the collection) and "eldest" (the "name" attribute of the document with the greatest "age").:

`[ { $sort: { age: -1 } }, { $group: { total: { $sum: 1 }, eldest: { $first: "$name"} } }, ]`

Pipelines can make use of operators, like $sum and $first in the example above. [learn more](http://docs.mongodb.org/manual/reference/operator/aggregation/#aggregation-pipeline-operator-reference)

Click the ... icon to open the MongoDB Pipelines Editor that can be used to construct your aggregation pipeline.

#### Result type <a href="#result-type" id="result-type"></a>

The _Type_ that will be used to store the results from the database. The names of the fields in the custom type should match the field names of the documents returned from MongoDb.

The Type can optionally include a string field named "id" which will be populated by the document's unique [id attribute](#what-are-id-attributes).

#### Return options <a href="#return-options" id="return-options"></a>

* _First row_\
  The function will return the first document returned by the query. If no data is returned by the query, an error will be reported.
* _First row, else empty row_\
  The function will return the first document returned by the query. If no data is returned by the query, the function will return a document containing default values.
* _List of rows_\
  The function will return all documents in one list. The list can then be used later in the process without having to execute the query again.
* _Row by row_\
  The function will automatically return one document at a time. You will see a "ForEachRow" loop icon as a child of this function. Any function you attach to the results will be inside of the loop. This is recommended whenever you expect to retrieve multiple items, but you don't need the complete list of items all at once.

### What are ID Attributes? <a href="#what-are-id-attributes" id="what-are-id-attributes"></a>

Each document in a Mongo collection is uniquely identified by an Object ID field called "\_id". When reading from the database, it is possible to retrieve this id by adding a [string](../../linx/content/string) field named "id" or "\_id" to the Type specified in the[ result type](#result-type) property.

When using the object id as an element in the Query property, the [string](../../linx/content/string) value will need to be converted back into a mongo object id. For example, a document with id "525bc19331eec126ecdcf199" can be retrieved using the query:

`{ _id: ObjectId("525bc19331eec126ecdcf199") }`

***

### Links <a href="#links" id="links"></a>

[Connection string format](http://docs.mongodb.org/manual/reference/connection-string/)

[Aggregation pipeline](http://docs.mongodb.org/manual/core/aggregation-pipeline/)

[Aggregation operators](http://docs.mongodb.org/manual/reference/operator/aggregation/#aggregation-pipeline-operator-reference)

[Query documents](http://docs.mongodb.org/manual/tutorial/query-documents/)

[Object Ids](http://docs.mongodb.org/manual/reference/object-id/)
