问题描述
我能够不考虑大小写而查询值,但是我想以不敏感的方式查询键,因此用户可以使用所有小写字母键入它们.
I am able to query values without regard to case, but I would like to to query keys insensitively, so users can type them in all lower case.
这无效,因为它不是有效的JSON:
This doesn't work, because it is not valid JSON:
{
/^lastName$/i: "Jones"
}
除了仅将键作为值的新集合之外,是否有一种策略可以用于此目的?
Is there a strategy I could use for this, besides just making a new collection of keys as values?
推荐答案
目前尚无办法.
MongoDB是无模式" 的,但不应与没有模式" 混淆.有一个隐含的假设,即您的代码可以控制实际出现在系统中的键的名称.
MongoDB is "schema-free" but that should not be confused with "doesn't have a schema". There's an implicit assumption that your code has some control over the names of the keys that actually appear in the system.
让我们来讨论这个问题.
Let's flip the question around.
- 用户插入区分大小写的键是否有充分的理由?
- 您能否在插入所有键时将所有键都转换为小写?
- Is there a good reason that users are inserting case-sensitive keys?
- Can you just cast all keys to lower-case when they're inserted?
同样,MongoDB假定您对可用键有所了解.您的问题意味着您不知道可用的密钥.您需要缩小这一差距.
Again, MongoDB assumes that you have some knowledge of the available keys. Your question implies that you have no knowledge of the available keys. You'll need to close this gap.
这篇关于MongoDB不区分大小写的键搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!