user4281200
user4281200

Reputation: 31

Using Chinese Characters with Pymongo

def readTags(collection,index):
    file = open("F:\\dship\\odps-dship\\test_tag_data.txt")
    while True:
        line = file.readline()
        if not line:
            break
        strArr = line.split("\t")[8].split(" ")
        for index in range(len(strArr)):
            #queryTag = strArr[index]
            print(collection.find_one({"tagList":{"$all",u"手拿包"}}))

when my program executed ,I get the follow exception

bson.errors.InvalidDocument: Cannot encode object: set(['$all', u'\u624b\u62ff\u5305'])

how can i resolve it?

Upvotes: 0

Views: 245

Answers (1)

Neil Lunn
Neil Lunn

Reputation: 151092

Python syntax for object notation is almost exactly the same as JSON notation as is commonly used in all the MongoDB official documentation. The only exception is "ordered dicts" where that is actually applied but it does not apply in this case since there are no keys that require a specific order.

So it's basically just following what the documentation for $all actually says, and nothing to do with the Unicode support whatsoever:

print(collection.find_one({ "tagList": { "$all": [ u"手拿包" ] } }))

Is the correct form of the line that is actually throwing the error for you. Your MongoDB syntax was wrong, and nothing to do with Chinese characters.

Upvotes: 2

Related Questions