Executing Queries in MongoDB with Greek Characters

2019-07-27 05:31发布

I am building an HTML5 app combining the AngularJS framework and MongoDB. The setup is similar to the ‘Wire up a backend’ demo in the AngularJS home page. So far, I have managed to save a large amount of documents in a single simply - structured MongoDB collection (hosted on Mongolab). These documents contain keys with latin characters and values with greek characters or numeric ones:

{ "name": "Νίκος", "value": 1.35}

I am pretty sure these documents are utf-8 encoded. The problem is that when I try to query the database with JS, passing strings containing greek characters, I get zero results.

var queryString = "{\"name\": \"Νίκος\"}";
$scope.query_results = Project.query({q: query_string}, null, $scope.query_success);

The same queries using php return the correct results. All other queries with numeric values or Latin characters are successfully executed (either from php or js). So the only problem is when I try to query the db through js using greek characters.

I have checked the encoding of the js documents to be utf-8 and I have set the html meta charset attribute to utf-8. I have also tried encoding the query string to utf-8 before querying the database, with no success though.

Any ideas? Thanks.

1条回答
做个烂人
2楼-- · 2019-07-27 06:11

Works for me from the shell (I copied your example document to insert, and then copied from the query for name), so at least you're not having one of those issues where the utf-8 characters look the same but are slightly different:

> db.test.insert({ "name": "Νίκος", "value": 1.35});
> db.test.find({name: "Νίκος"});
{ "_id" : ObjectId("4f9b1642c26c79dac82740c5"), "name" : "Νίκος", "value" : 1.35 }

Double check your file encoding on the js file? Although, I'm sure in your real program, you have that search value coming from a URL encoded form through GET or POST, so the encoding on the js file wouldn't matter.

You might try setting accept-charset="utf-8" in your form. If it's AJAX or posted through JS via the angular bindings, make sure that the character encoding is set before you send it as well. Something like this? http://groups.google.com/group/angular/browse_thread/thread/e6701e749d4bc8ed

查看更多
登录 后发表回答