0

I'm slowly getting desperate about this. I am trying to get a very specific entry from a previously defined database. For this I have defined my database:

test = cluster["folder1"]["example"]

In this there are keys with a value, that looks something like this:

Database picture

Now I want to use the find_one() or find() method to find this entry. There are more entries in the database.

I already managed to go over the collection with a loop and the entry is also displayed:

def get_key(guild, answer):
    for x in test.find({}, {f"{answer}"}): # The searched "answer" is defined before by a command, not needed for this matter
        print(x)

EDIT: Maybe I need to mention this: "answer" can also have a different value, for example "quantum" which is then maybe "assigned" to "This is quantum" or "finger" which is "assigned" to "This is a finger". Therefore, I can't use test.find_one({"test: "FAILUR"}) and need to find a way to find the value for the key "answer"

It will look like this:

{'_id': ObjectId('Removed')}
{'_id': ObjectId('Removed')}
{'_id': ObjectId('Removed'), 'test': 'FAILUR'} # I just need FAILUR

I would now like to display "FAILUR" and not always use a for loop. Is this therefore much easier and faster with the find_one() method?

Already I tried:

    print(test.find_one({f"{answer}": f"{answer}"})) # Outputs None
    print(test.find({}, {f"{answer}"})) # prints <pymongo.cursor.Cursor object at 0x7fc6d482XXX>

Additionally, I looked at the following posts, but just didn't get anywhere:

https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/ https://docs.mongodb.com/manual/reference/method/db.collection.findOne/

How to select a single field for all documents in a MongoDB collection?

How to read a specific key-value pair from mongodb collection

And much more. Thanks in advance!

R6Winner
  • 29
  • 7
  • `test.find_one({f"{answer}": f"{answer}"}` - I think you're on the right track, but key and value are the same here. So if `answer` is `"test"`, it will be equivalent to `find_one(["test": "test"})` – Sergio Tulentsev Jan 07 '22 at 15:39
  • @SergioTulentsev Yes, I guess that was just me trying to test what is possible or not. Somehow, I can't find the way to only require `answer` and then get the `value` for it... – R6Winner Jan 07 '22 at 15:41
  • Look up operator `$exists` – Sergio Tulentsev Jan 07 '22 at 15:50
  • @SergioTulentsev Thanks. This leads me closer to what I am looking for. Now I just need to find a way to extract the `value` out of it. `print(test.find_one({f"{answer}": {"$exists": True}}))` now returns what my `for`-loop did earlier. – R6Winner Jan 07 '22 at 16:02
  • Not quite the same, though. This one does not include documents where answer is not present. – Sergio Tulentsev Jan 07 '22 at 16:13
  • @SergioTulentsev Correct me if I did not understand you right, but that is what I want. All other documents that do not match my search criteria should be left out. This is what the code does, at least it printed the correct thing. – R6Winner Jan 07 '22 at 16:35
  • Just to add up on this. I can get the content by defining my `test.find_one()` and get the `f"{answer}"` out of it. I do not know if there is an easier way but it works for now. – R6Winner Jan 07 '22 at 16:51

1 Answers1

0
test.find( { "test": "FAILUR" } )

This would only return the documents where the field test has the value of FAILUR

sandeep.kgp
  • 722
  • 1
  • 7
  • This will find it for sure, yes, but `answer` can also be different, depends on the command. Imagine it like this: One time I need to search for "test" and another time I need to search for "quantum", therefore I need something that displays the value, based on "test" or "quantum". – R6Winner Jan 07 '22 at 15:35
  • so you mean you want all the documents where any `key` has value `FAILUR` ? – sandeep.kgp Jan 07 '22 at 15:36
  • could you explain the exact use case? – sandeep.kgp Jan 07 '22 at 15:37
  • I only want to look in that collection for the match I define. For example I define "people" as the "answer" then I want my bot to find the `value` for the `key` "people". – R6Winner Jan 07 '22 at 15:40