如何在mongodb数据库中插入一个字符串列表?

时间:2013-03-16 07:08:20

标签: python flask pymongo

database = mongo_connect()
un = str(session['username'])

database.game.insert({'host': session['username'],
    'player_list':[un]})

如果我这样做,那么当我检索player_list时,我会得到一个unicodes列表。我怎样才能得到一个字符串列表?感谢

1 个答案:

答案 0 :(得分:1)

因为mongodb stores data in bson format, and since bson is utf8-encoded, you'll get only unicode strings

无论如何,您可以将unicode编码为str;

player_list = [x.encode('utf-8') for x in player_list]