“Connectez MongoDB à Python” Réponses codées

Connectez MongoDB à Python

from pymongo import MongoClient
# pprint library is used to make the output look more pretty
from pprint import pprint

# connect to MongoDB, change the << MONGODB URL >> to reflect 
#your own connection string
client = MongoClient(<<MONGODB URL>>)
db=client.admin
# Issue the serverStatus command and print the results
serverStatusResult=db.command("serverStatus")
pprint(serverStatusResult)

#We can now create a database object referencing a new database, 
#called “business”, as follows:

db = client.business
MitchAloha

Comment connecter la base de données MongoDB avec Python

def get_database():
    from pymongo import MongoClient
    import pymongo

    # Provide the mongodb atlas url to connect python to mongodb using pymongo
    CONNECTION_STRING = "mongodb+srv://<username>:<password>@<cluster-name>.mongodb.net/myFirstDatabase"

    # Create a connection using MongoClient. You can import MongoClient or use pymongo.MongoClient
    from pymongo import MongoClient
    client = MongoClient(CONNECTION_STRING)

    # Create the database for our example (we will use the same database throughout the tutorial
    return client['user_shopping_list']
    
# This is added so that many files can reuse the function get_database()
if __name__ == "__main__":    
    
    # Get the database
    dbname = get_database()
Rawand Lazez

Réponses similaires à “Connectez MongoDB à Python”

Questions similaires à “Connectez MongoDB à Python”

Plus de réponses similaires à “Connectez MongoDB à Python” dans Python

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code