简体   繁体   中英

How to address MongoDB database with hyphen in name in Python?

I need to remove any element from a Mongo database when I have two databases:

  1. mydatabase
  2. data-test-second

With the first database this isn't a problem, I use MongoClient:

self.db = self.client.mydatabase
result = self.db.test.delete_one({"name": 'testelement'})

bBt when I use this for a second database I have a problem with:

self.db = self.client.data-test-second 

underlining the database name, how I can write this? Or I can't use this solution for the second name?

In the case that your database name is not valid as an object name in Python, you need to address the database differently:

self.db = self.client["data-test-second"]

In general, it is probably advisable to always use this pattern.

For more information, you can refer to the documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM