Skip to main content

The MongoDB connector supports read and writing to/from a collection.

Tabset

Read​

Recipe​

read:
- mongodb:
user: user
password: password
database: database
collection: collection
host: host # mongoDB cluster-url
query: query

# Optional
projection: projection

Function​

from wrangles.connectors import mongodb
df = mongodb.read(
user='user',
password='password',
database='db',
host='cluster123.mongodb.net',
query='{"name": "WrWx"}',
projection='{"_id": 0, "name": 1}'
)

Parameters​

ParameterRequiredData TypeNotes
user✓strUser with access to the database
password✓strPassword of user
database✓strDatabase to be queried
collection✓strCollection to be queried
host✓strmongoDB cluster-url
query✓strmongoDB query
projectionstrSelect which fields to include
order_bystrUses SQL syntax to sort the input.
ifstrA condition that will determine whether the action runs or not as a whole.

Write​

Recipe​

write:
- mongodb:
user: user
password: password
database: database_write
collection: collection_write
host: host # mongoDB cluster-url
action: action

# Optional
columns:
- column1
- column2

Function​

from wrangles.connectors import mongodb
mongodb.write(
df,
user='user'
password='password'
database='db'
host='cluster123.mongodb.net'
action: 'INSERT'
)

Parameters​

ParameterRequiredData TypeNotes
df✓DataFrameFunction only. DataFrame of contents to write to the database. Columns must match the target schema. By default, if the target collection doesn't exist, it will be created. DataFrame orient is set to 'records'.
user✓strUser with access to the database
password✓strPassword of user
database✓strDatabase to be queried
collection✓strCollection to be queried
host✓strmongobd cluster-url
action✓stractions supported INSERT, UPDATE
querystrmongoDB query to search for value to update, only valid when using UPDATE
updatestrmongoDB query value to update, only valid when using UPDATE
order_bystrUses SQL syntax to sort the output.
ifstrA condition that will determine whether the action runs or not as a whole.