Pymongo: iterate over all documents in the collection

后端 未结 4 1310
孤城傲影
孤城傲影 2021-02-13 18:26

I am using PyMongo and trying to iterate over (10 millions) documents in my MongoDB collection and just extract a couple of keys: \"name\" and \"address\", then output them to .

4条回答
  •  旧巷少年郎
    2021-02-13 18:35

    I had no luck with .find().forEach() either, but this should find what you are searching for and then print it.

    First find all documents that match what you are searching for

    cursors = db.myCollection.find({"name": {$regex: REGEX}})
    

    then iterate it over the matches

    for cursor in cursors
        print(cursor.get("name"))
    

提交回复
热议问题