I got documents from MongoDB. I want to print this documents as a list of dictionary using Python. How can I do it?

나의 재물운? 연애운은?

AI가 봐주는 내 사주팔자 - 운세박사

https://aifortunedoctor.com/

I got documents from MongoDB. I want to print this documents as a list…

영리치 0 985 2023.02.14 14:40

I got documents from MongoDB. I want to print this documents as a list of dictionary using Python. How can I do it?


Assuming you have retrieved the documents from MongoDB using a library such as PyMongo, you can iterate over the result cursor and create a list of dictionaries.

Here is an example code snippet:


import pymongo # Create a client to connect to MongoDB client = pymongo.MongoClient("mongodb://localhost:27017/") # Select a database and collection db = client["mydatabase"] collection = db["mycollection"] # Retrieve documents from the collection documents = collection.find() # Create a list of dictionaries from the documents result = [] for document in documents: result.append(dict(document)) # Print the result print(result)


In this code, collection.find() returns a cursor object which can be iterated to retrieve each document. Each document is then converted to a dictionary using the dict() function and added to a list. Finally, the list is printed.

Note that this code assumes that each document can be converted to a dictionary. If your documents have nested fields or other complex data types, you may need to perform additional processing to convert them to a flat dictionary.



I used your method, but the FastAPI replied error message ValueError: [TypeError("'ObjectId' object is not iterable"), TypeError('vars() argument must have __dict__ attribute')]


The error message you're seeing suggests that there might be an issue with the type of data you're trying to convert to a dictionary.

It's possible that one or more of the documents retrieved from MongoDB contain fields that cannot be directly converted to a dictionary, such as ObjectId objects.

To work around this issue, you can manually convert the fields that are causing the problem to a string or another appropriate type before converting the entire document to a dictionary. Here's an example of how you could modify the code to do this:


import pymongo # Create a client to connect to MongoDB client = pymongo.MongoClient("mongodb://localhost:27017/") # Select a database and collection db = client["mydatabase"] collection = db["mycollection"] # Retrieve documents from the collection documents = collection.find() # Create a list of dictionaries from the documents result = [] for document in documents: # Convert ObjectId to string document['_id'] = str(document['_id']) # Convert other problematic fields to appropriate types or strings # ... # Convert entire document to dictionary result.append(dict(document)) # Print the result print(result)



In this example, we convert the '_id' field from an ObjectId to a string using the str() function. If you have other fields that are causing problems, you can similarly convert them to appropriate types or strings.

By doing this, we avoid any problematic data types that may be causing the error.




990564053_ADcqRHI2_958d4d6d3fc6ed6b4b83c844a8eb3ddb37682fff.png



990564053_PS4OWjo5_f1e2b8aa1cd98b408be65d02db0a831d3c52bf52.png






Comments

나의 재물운? 연애운은?

AI가 봐주는 내 사주팔자 - 운세박사

https://aifortunedoctor.com/

Category
Magazine
훈남/훈녀
 
 
 
상점
Facebook Twitter GooglePlus KakaoStory NaverBand