English 中文(简体)
MongoDB: Query over a h with special chars in keys
原标题:MongoDB: Query over a hash with special chars in keys

我开始与MongoDB合作(看见AWESOME、BTW),我对我的一件收藏中哈希族数据类型有疑问。 从理论上讲,我有这样的想法:

{
    "account_id": "BNM-X10-0123456789",
    "account_profile": {
        "Client code": "0123456789",
        "Discount %": "15"
    }
}

如你所知,<代码>账户>中的关键内容是。 has有一些白色空间和特殊char! 我知道,你可能建议“适当改动其编号为client_codediscount_pct”,但我没有控制这些钥匙,这些钥匙完全是用户定义的,因此,按性质来说,这些钥匙包括白天空间和特殊char。

So, the original question is: how can I perform queries over that info? I —obviously— know that the dot notation as in db.foo.find({"account_profile.discount_pct": "15"}) will not work, but are there alternatives?

作为一个次要问题,在收集过程中保持所有信息封顶的同时,能否更好地完成同样的功能?

感谢:

最佳回答

根据以下来源,you可以在实地使用UTF8特性: 唯一的例外是: 外地名称不允许的特性(因为它用于查询次文件),而外地名称则可能以美元为首。

http://www.hchr.org。

https://jira.mongodb.org/browse/SERVER-3229

问题回答

如果你知道你可以这样说的话,你就只能问一下。

db.foo.find({ account_profile.discount_pct  :  15 })

检查测试数据

> db.foofoo.insert({name: ram ,account_profile : {"Client code": "0123456789", discount_pct  : 2}})
> db.foofoo.insert({name: ram ,account_profile : {"Client code": "0123456789", discount_pct  : 2}})
> db.foofoo.insert({name: ram ,account_profile : {"Client code": "01236789", discount_pct  : 5}})
> db.foofoo.insert({name: ram ,account_profile : {"Client code": "01236789", discount_pct  : 2}})
> db.foofoo.insert({name: ram ,account_profile : {"Client code": "01236789", discount %  : 2}})
> db.foofoo.insert({name: ram ,account_profile : {"Client code": "01236789", discount_pct  : 4}})
> db.foofoo.insert({name: ram ,account_profile : {"Client code": "01236789", discount_%  : 4}})
> db.foofoo.insert({name: ram ,account_profile : {"Client code": "01236789", discount_%  : 2}})
> db.foofoo.find({ account_profile.discount_% : 2})
{ "_id" : ObjectId("4eb0c9965325a7760cfda3db"), "name" : "ram", "account_profile" : { "Client code" : "01236789", "discount_%" : 2 } }
> db.foofoo.find({ account_profile.discount_pct : 2})
{ "_id" : ObjectId("4eb0c9725325a7760cfda3d5"), "name" : "ram", "account_profile" : { "Client code" : "0123456789", "discount_pct" : 2 } }
{ "_id" : ObjectId("4eb0c97c5325a7760cfda3d7"), "name" : "ram", "account_profile" : { "Client code" : "01236789", "discount_pct" : 2 } }

由于上述,,你有时间。 外地名称,因为它代表了公证。





相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签