3

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?

2
  • its hard to follow your code. Can you explain it a bit more please? I think your issue is that you overwrite the first db instance with your second assignment. Commented Feb 5, 2020 at 12:20
  • self.db = self.client.data - test - second is a bad example. Please extract and provide a minimal reproducible example. Also, you need to descibe what problem you have, i.e. what you observed when running the example code. As a new user here, also take the tour and read How to Ask. Commented Feb 5, 2020 at 12:26

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, can you tell me how I can create a database from code ? example new database name: "testing123" how write this ?
Please note that the comments are not for asking new questions. Please refer to the documentation or search StackOverflow for the answer to your new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.