I am working on a project with Flask and Mongo DB and I want to display some embedded documents. I am not really getting error messages but the data is not displaying how I want it to. Currently, my documents look like this:
{ "_id" : ObjectId("590639009103ad05fd8555dc"),
"comments" : [ { "comment" :
"Hello World" } ],
"age" : 23,
"name" : "Mike" }
Now, I want to display data that will show the name and the comments that the individual said. I want something like this: Mike, says the following: 'Hello World'
My code looks like this:
thoughts = person.show()
for thought in thoughts:
print(thought["name"], "says the following:", thought["comments"])
where the show() method looks like this:
def show(self):
thoughts = self.db.people.find()
return thoughts
Now for the most part almost everything works how I want it to. When I run my code I get this: Mike says the following: {'comment': 'Hello World'}
What I need to do is to dig further down into the embedded document to display:
Mike, says the following: 'Hello World'
I have tried the following:
for thought in thoughts:
print(thought["name"], "says the following:",
thought["comments.comment"])
which gets me the following error message: KeyError: 'comments.comment'
I then tried the following:
for thought in thoughts:
print(thought["name"], "says the following:", thought["comments"]
["comment"])
which gives me the following error: TypeError: list indices must be integers, not str
Thus, I am somewhat stuck how to pull out each comment. Any help would be appreciated.