0

I've got the following problem. I don't seem to figure out on how to access the "UID" value within the warning field. I want to iterate over every value inside the document, and access every existing UID to check if a randomly generated UID already exists. I'm honestly about to have a mental breakdown due this, I just can't seem to figure it out

This is what my MongoDB structure looks like: https://i.stack.imgur.com/RmdMT.png

down bad
  • 21
  • 1
  • 3
  • Can you please read about [the problems with images of text](//meta.stackoverflow.com/a/285557/11107541) and then [edit] to **add transcriptions** of your images of text as actual text? Perhaps useful: [/help/formatting](/help/formatting). (I'm assuming that's an image of BSON?) – starball Jan 15 '23 at 22:56

1 Answers1

0

warnings will be a list after you've got the object in python code - so you can just iterate over the docs in the warnings list and access their UID keys

edited code for comments: We can get all the documents in the collection by using find with an empty query dict, though I've linked the docs on find below. As your document structure seems to have a random number as a key, and then an embedded doc, we find the keys in the document that aren't _id. It may be that there's only one key, but I didn't want to assume. If there's then a warnings key in our embedded doc, we iterate over each of the warnings and add the "UID" to our warning_uids list.

# this is your collection object
mod_logs_collection = database["modlogs"]

all_mod_docs = mod_logs_collection.find({})

warning_uids = []
for doc in all_mod_docs:
    # filter all the doc keys that aren't the default `_id`
    doc_keys = [key for key in doc.keys() if key != "_id"]

    # not sure if it's possible for you to have multiple keys in a doc 
    # with this structure but we'll iterate over the top-level doc keys
    # just in case
    for key in doc_keys:
        sub_doc = doc[key]
        if warnings := sub_doc.get("warnings")
            for warning in warnings:
                warning_uids.append(warning["UID"])
                print(warning["UID"])
ESloman
  • 1,961
  • 1
  • 10
  • 14
  • Hi, first of all thanks for the answer. The problem here now is that I'm not sure on how to access the document which contains the warnings key. That's what the main issue is about – down bad Jan 15 '23 at 20:08
  • How are you using `pymongo`? Are you already querying the collection somewhere? – ESloman Jan 15 '23 at 20:11
  • I am aware on how to access the collection, yes. In this case it's named `Modlogs` and I declared it as a variable therefore I can use Modlogscollection throughout the whole file, the variable is declared like this: `Modlogscollection = database["modlogs"]`. Printing out Modlogscollection gives me this output: – down bad Jan 15 '23 at 20:17
  • I've added a bit of that to the answer above now - hopefully that helps you along a little further! Hopefully it's clear enough too. – ESloman Jan 15 '23 at 20:33
  • No worries - glad to help. Please mark the answer as accepted when you can :) – ESloman Jan 15 '23 at 21:02