MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

Come scaricare una raccolta su un file json usando pymongo

Prendi tutti i documenti e salvali su un file, ad esempio:

from bson.json_util import dumps
from pymongo import MongoClient

if __name__ == '__main__':
    client = MongoClient()
    db = client.db_name
    collection = db.collection_name
    cursor = collection.find({})
    with open('collection.json', 'w') as file:
        file.write('[')
        for document in cursor:
            file.write(dumps(document))
            file.write(',')
        file.write(']')