查询键中的 MongoDB 通配符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6179871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:06:24  来源:igfitidea点击:

MongoDB wildcard in the key of a query

mongodbmongodb-querywildcard

提问by Brad

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-267but it looks like it's not been resolved.

是否可以通配查询中的键?例如,鉴于以下记录,我想做一个.find({'a.*': 4})这里讨论过的https://jira.mongodb.org/browse/SERVER-267但看起来它没有得到解决。

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

采纳答案by Gates VP

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.

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

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

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

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

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

那么“通配符”在这里做什么?db.foo.find( { a.* : 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.

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

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.

是否有您想要实现的特定语义?也许文档结构的改变会让你得到你想要的查询。