Hi all, quick question: I can't find a mongodb cru...
# ask-community
r
Hi all, quick question: I can't find a mongodb crud task. Have any of you guys have something that works?
a
Have you used pymongo? To insert a document into collection you could do sth as simple as that:
Copy code
from prefect import Flow, task
import pymongo


@task(log_stdout=True)
def insert_document():
    myclient = pymongo.MongoClient("<mongodb://localhost:27017/>")
    mydb = myclient["mydatabase"]
    mycol = mydb["users"]
    mydict = {"name": "Marvin", "address": "Chicago 42"}
    x = mycol.insert_one(mydict)


with Flow("flow_pymongo") as flow:
    hw = insert_document()

if __name__ == "__main__":
    flow.run()
And if you are dealing with (e.g. financial) time series data, definitely check out arctic! makes it easy to insert an entire Pandas dataframe into MongoDB while keeping things like pandas index https://github.com/man-group/arctic
r
Tnx Anna for the quick reply! I'm using motor and beanie, but can simply follow your example! Didn't know arctic. Looking great. 🙂
👍 1
a
absolutely, I'm a fan myself 🙂