# mongodb – lookup info from MongoDB

- Synopsis
- Requirements
- Parameters
- Notes
- Examples
- Status

## Synopsis

- The `MongoDB` lookup runs the find() command on a given collection on a given MongoDB server.
- The result is a list of jsons, so slightly different from what PyMongo returns. In particular, timestamps are converted to epoch integers.

## Requirements

The below requirements are needed on the local master node that executes this lookup.

- pymongo >= 2.4 (python library)

## Parameters

Parameter | Choices/Defaults | Configuration | Comments
--- | --- | --- | ---
collection - / required |  |  | Name of the collection which the query will be made
connect_string - | Default: "mongodb://localhost/" |  | Can be any valid MongoDB connection string, supporting authentication, replicasets, etc. More info at https://docs.mongodb.org/manual/reference/connection-string/
database - / required |  |  | Name of the database which the query will be made
filter dictionary | Default: "{}" |  | Criteria of the output
limit integer |  |  | How many results should be shown
projection dictionary | Default: "{}" |  | Fields you want returned
skip integer |  |  | How many results should be skipped
sort list | Default: "[]" |  | Sorting rules. Please notice the constats are replaced by strings.

## Notes

Note

- Please check https://api.mongodb.org/python/current/api/pymongo/collection.html?highlight=find#pymongo.collection.Collection.find for more details.

## Examples

    - hosts: all
      gather_facts: false
      vars:
        mongodb_parameters:
          #mandatory parameters
          database: 'local'
          #optional
          collection: "startup_log"
          connection_string: "mongodb://localhost/"
          extra_connection_parameters: { "ssl" : True , "ssl_certfile": /etc/self_signed_certificate.pem" }
          #optional query  parameters, we accept any parameter from the normal mongodb query.
          filter:  { "hostname": "batman" }
          projection: { "pid": True    , "_id" : False , "hostname" : True }
          skip: 0
          limit: 1
          sort:  [ [ "startTime" , "ASCENDING" ] , [ "age", "DESCENDING" ] ]
      tasks:
        - debug: msg="Mongo has already started with the following PID [{{ item.pid }}]"
          with_mongodb: "{{mongodb_parameters}}"

## Status

- This lookup is not guaranteed to have a backwards compatible interface. [preview]
- This lookup is maintained by the Ansible Community. [community]

### Authors

- Marcos Diez <marcos (at) unitron.com.br>

Hint

If you notice any issues in this documentation, you can edit this document to improve it.

Hint

Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.