查询键中的 MongoDB 通配符 [英] MongoDB wildcard in the key of a query

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

问题描述

是否可以在查询中使用通配符?例如,给定以下记录,我想做一个 .find({'a.*': 4})这在此处讨论 https://jira.mongodb.org/browse/SERVER-267 但它看起来像没有解决.

Is it possible to wildcard the key in a query? For instance, given the following record, I'd like to do a .find({'a.*': 4}) This was discussed here https://jira.mongodb.org/browse/SERVER-267 but it looks like it's not been resolved.

{
  'a': {
    'b': [1, 2],
    'c': [3, 4]
  }
}

推荐答案

正如所问,这是不可能的.您链接的服务器问题仍属于我们不确定的问题".

As asked, this is not possible. The server issue you linked to is still under "issues we're not sure of".

MongoDB 有一些关于数组使用的智能,我认为这是围绕这种功能的复杂性的一部分.

MongoDB has some intelligence surrounding the use of arrays, and I think that's part of the complexity surrounding such a feature.

采用以下查询 db.foo.find({ 'a.b' : 4 } ).此查询将匹配以下文档.

Take the following query db.foo.find({ 'a.b' : 4 } ). This query will match the following documents.

{ a: { b: 4 } }
{ a: [ { b: 4 } ] }

那么通配符"在这里做什么?db.foo.find( { a.* : 4 } ) 是否匹配第一个文档?第二个呢?

So what does "wildcard" do here? db.foo.find( { a.* : 4 } ) Does it match the first document? What about the second?

此外,这在语义上是什么意思?正如您所描述的,查询实际上是查找该文档中任何字段的值为 4 的文档".这有点不寻常.

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 通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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