Home > Net >  Retrieving JSON data from MongoDB with Hebrew values using Python
Retrieving JSON data from MongoDB with Hebrew values using Python

Time:10-29

I'm new to MongoDB - and I'm having this problem when I retrieve a key from my JSON based MongoDB - it cannot parse Hebrew key values and it returns them blank when I read them.

I had this problem before when I read and wrote to the JSON file locally, using UTF-8 encoding solved this problem like in this example:

with codecs.open('words.json', 'wb', encoding='utf-8') as file:
        json.dump(data, file, ensure_ascii=False)

So my question is, is there a way to retrieve MongoDB data using utf-8 encoding? i'm currently using find() to read my data.

like that:

for x in mycol.find():
  topic = 'בדיקה'
  print(x[topic])

Thanks in advance!

CodePudding user response:

I can't reproduce your error. This snippet seems to work for me:

import pymongo

mycol = pymongo.MongoClient()['mydatabase']['mycol']

mycol.delete_many({})
mycol.insert_one({'בדיקה': 'בדיקה'})

for x in mycol.find():
    topic = 'בדיקה'
    print(x[topic])

prints:

בדיקה

CodePudding user response:

As the nice guy above me tried - it was indeed my workspace environment that caused the problem for some reason - if you ever face this kind of problem with mongodb, simple try using a clean different work environment and console to see if it fixed the issue.

  • Related