MongoDB wildcard in the key of a query

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.

Leave a Comment