MongoDB中执行的查询与使用Javascript希腊字符不返回任何结果(Executing Qu

2019-10-16 16:07发布

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.

Answer 1:

从shell工作对我来说(我复制你的示例文档中插入,然后从查询名称复制),这样至少你不会有这些问题在UTF-8字符看起来相同,但略有不同的一个:

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

仔细检查的js文件的文件编码? 虽然,我在你的真正的程序敢肯定,你有搜索值从通过GET或POST一个URL编码的形式到来,所以对JS文件的编码也没有什么关系。

你可以尝试设置accept-charset="utf-8"在你的形式。 如果通过斜绑定的Ajax或张贴通过JS,确保你把它和之前的字符编码设置。 像这样的事情? http://groups.google.com/group/angular/browse_thread/thread/e6701e749d4bc8ed



文章来源: Executing Queries in MongoDB with Greek Characters using Javascript Returns No Results