python connects to the mongodb password authentication instance

  • 2020-12-21 18:05:39
  • OfStack

As shown below:


from pymongo import MongoClient
# Establish a connection to the database system , The specified host and port parameter 
client = MongoClient('localhost', 27017)
# The connection mydb The database , Account password authentication 
db = client.mydb
db.authenticate("account", "password")
# Join table 
collection = db.myset


# View all table names 
db.collection_names()
print db.collection_names()
 
# Access table data , Filter query 
item = collection.find({},{"name":1,"age":21})
for rows in item:
    print rows.values()
 
# Access to the table 1 Rows of data 
print collection.find_one()

Related articles: