<pre><code>import pymongo
from bson.objectid import ObjectId
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
db = myclient["test"]
comments_col = db["comments"]
doc = {
"post_body": "test",
"author": "test1",
"comments": [
{
"comment_body": "comment test",
"comment_author": "test2",
"replies": [
{
"reply_body": "reply test1",
"reply_author": "test3"
},
{
"reply_body": "reply test2",
"reply_author": "test2"
},
]
}
]
}
def insert(doc1):
id_comment = comments_col.insert(doc1)
return id_comment
def find(id_comment):
comment = comments_col.find_one({"_id": ObjectId(str(id_comment))})
return comment
if __name__ == "__main__":
id_comment = insert(doc)
comment = find(id_comment)
print("comments with replies : ", comment["comments"])
print("\n")
print("replies : ", comment["comments"][0]["replies"])
print("\n")
print("comment_author : ", comment["comments"][0]["comment_author"])
print("\n")
print("comment_body : ", comment["comments"][0]["comment_body"])
</code></pre>