查询键中的 MongoDB 通配符
是否可以在查询中使用通配符键?例如,给定以下记录,我想要执行 .find({'a.*': 4})
这已在此处讨论 https://jira.mongodb.org/browse/SERVER-267 但看起来还没有解决。
{
'a': {
'b': [1, 2],
'c': [3, 4]
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如所问,这是不可能的。您链接到的服务器问题仍然位于“我们不确定的问题”下。。
MongoDB 有一些关于数组使用的智能,我认为这是围绕这种功能的复杂性的一部分。
采用以下查询
db.foo.find({ 'ab' : 4 } )
。该查询将匹配以下文档。那么“通配符”在这里做什么呢?
db.foo.find( { a.* : 4 } )
与第一个文档匹配吗?第二个呢?此外,这在语义上意味着什么?正如您所描述的,查询实际上是“查找该文档中任何字段的值为 4 的文档”。。这有点不寻常。
您是否想要实现特定的语义?也许文档结构的改变会给你带来你想要的查询。
As asked, this is not possible. The server issue you linked to is still under "issues we're not sure of".
MongoDB has some intelligence surrounding the use of arrays, and I think that's part of the complexity surrounding such a feature.
Take the following query
db.foo.find({ 'a.b' : 4 } )
. This query will match the following documents.So what does "wildcard" do here?
db.foo.find( { a.* : 4 } )
Does it match the first document? What about the second?Moreover, what does this mean semantically? As you've described, the query is effectively "find documents where any field in that document has a value of 4". That's a little unusual.
Is there a specific semantic that you're trying to achieve? Maybe a change in the document structure will get you the query you want.
从 MongoDB v3.4+ 开始,您可以使用
$objectToArray
将a
转换为kv元组数组以供查询。这是 Mongo Playground 供您参考。
Starting from MongoDB v3.4+, you can use
$objectToArray
to converta
into an array of k-v tuples for querying.Here is the Mongo playground for your reference.
我遇到这个问题是因为我遇到了同样的问题。这里接受的答案提供者确实解释了为什么不支持这一点,但并没有真正解决问题本身。
我最终找到了一个解决方案,该解决方案使此处的通配符使用变得多余,并在此处共享,以防万一有人有一天会发现这篇文章
为什么我想在 MongoDB 查询中使用通配符?
就我而言,我需要这个“功能”以便能够在字典中找到匹配项(正如问题的代码所示)。
有什么替代方案?
使用反向映射(与 DNS 的工作方式非常相似)并简单地使用它。因此,在我们的例子中,我们可以使用与此类似的东西:
我知道,它需要更多内存,并且插入/更新操作应该验证该集合始终是对称的,但它解决了问题。现在,我知道,不是像我一样进行虚构的查询,而是
可以进行实际的查询
,它将返回具有特定值的所有项目(在我们的示例中
4
)- 这种方法需要更多的内存,您需要如果您想获得良好的性能,请正确管理索引(请阅读 docs)并且仍然 - 这对我的用例很有用。希望有一天这也能对其他人有所帮助
I've came across this question because I faced the same issue. The accepted answer provider here does explains why this is not supported but not really solves the issue itself.
I've ended up with a solution that makes the wildcard usage purposed here redundant and share here just in case someone will find this post some day
Why I wanted to use wildcards in my MongoDB queries?
In my case, I needed this "feature" in order to be able to find a match inside a dictionary (just as the question's code demonstrates).
What's the alternatives?
Use a reversed map (very similar to how DNS works) and simply use it. So, in our case we can use something similar to this:
I know, it takes more memory and insert / update operations should validate this set is always symmetric and yet - it solves the problem. Now, instead of making an imaginary query like
I can make an actual query
Which will return all items that have a specific value (in our example
4
)I know - this approach takes more memory and you need to manage indexes properly if you want to gain good performance (read the docs) and still - it's good for my use-case. Hope this helps someone else someday as well