MongoDB wildcard in the key of a query

By : Brad
Source: Stackoverflow.com
Question!

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]
  }
}
By : Brad


Answers
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.

{ 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?

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.

By : Gates VP


This video can help you solving your question :)
By: admin