Executing Queries in MongoDB with Greek Characters using Javascript Returns No Results

老子叫甜甜 提交于 2019-12-14 03:59:41

问题


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:


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



来源:https://stackoverflow.com/questions/10358302/executing-queries-in-mongodb-with-greek-characters-using-javascript-returns-no-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!