MongoDB:在键中查询具有特殊字符的哈希值 [英] MongoDB: Query over a hash with special chars in keys

查看:643
本文介绍了MongoDB:在键中查询具有特殊字符的哈希值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用MongoDB(它看起来很AWESOME,BTW),我有一个关于哈希数据类型在我的一个集合的问题。我理论上有这样的东西:

I'm starting to work with MongoDB (it looks AWESOME, BTW) and I have a question about a Hash data type in one of my collections. I theoretically have something that looks like this:

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

可以看到, account_profile 哈希中的键有一些白空格和特殊字符!我知道你可能会建议只是更改为 client_code discount_pct ,但我不能控制那些键,它们是完全用户定义的,并且在本质上它们的FOR SURE将包含空格和特殊字符。

As you can see, the keys in the account_profile hash has some white spaces and special chars! I know that you probably would suggest "just change it for client_code and discount_pct", but I do not have control over that keys, they are completely user defined and, by nature, they FOR SURE will contain white spaces and special chars.

因此,原来的问题是:查询该信息?我显然知道,在 db.foo.find({account_profile.discount_pct:15})中的点符号将无法工作,但有替代?

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?

作为一个侧面的问题,有没有更好的方法来完成相同的功能,同时保持所有的信息嵌套在集合中?

And as a side question, is there a better way to accomplish the same functionality while keeping all the info nested in the collection?

感谢y'all。 :)

Thank y'all. :)

推荐答案

您可以在字段名称中使用任何UTF8字符 - 唯一的例外是字符名称中不允许的。字符(因为它用于查询子文档),字段名不能以$字符开头。

according to the source below, you can use any UTF8 character in the field name -- the only exception is the '.' character which is not allowed in field names (because it's used to query sub-documents), and field names can't start with a '$' character..

请参阅:

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

这篇关于MongoDB:在键中查询具有特殊字符的哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆