Is it possible to search a wildcard sub-document key?

5,955 views
Skip to first unread message

Mackstar

unread,
Aug 23, 2011, 11:33:11 AM8/23/11
to mongodb-user
{ "_id" : ObjectId("4e53c49385708082b5020000"), "en" : { "name" :
"Richard", "profile" : "Dreaded Rasta"} }

Say I have the above sample record, (I am wanting to be able to add
different languages to each record). It is easy enough if I know which
locale I need to search as it would just be for example

{"en.name" : "Richard"}

But what if I didn't have a language preference or I didn't mind which
locale I was searching, how would I be able to do something like

{"*.name" : "Richard"}

If any one has any advice I would be very happy.

Thanks

Richard

Marc

unread,
Aug 23, 2011, 2:48:41 PM8/23/11
to mongodb-user
This is a very similar question to one that was just asked here:
http://groups.google.com/group/mongodb-user/browse_thread/thread/32b00d38d50bd858

The answer is the same. In a nutshell, values that unknown should
never be assigned as keys because it makes querying very inefficient.
There are no 'wild card' queries.

It is better to restructure the database such that values that are
unknown are not keys:

{ "_id" : ObjectId("4e53c49385708082b5020000"), "language":"en",
"name" : "Richard", "profile" : "Dreaded Rasta" }

If restructuring the database is not possible, a $where query may be
used with a for loop.

> db.people.find({ $where : function() {for(var unknownLanguage in this){if(this[unknownLanguage]["name"] == "Richard"){return true;}} return false;}});

Mackstar

unread,
Aug 24, 2011, 8:24:08 AM8/24/11
to mongodb-user
Hi Marc,

I was thinking this might be the case. I will be changing the schema
to accommodate this.

Thanks for the help

Richard

On Aug 23, 7:48 pm, Marc <m...@10gen.com> wrote:
> This is a very similar question to one that was just asked here:http://groups.google.com/group/mongodb-user/browse_thread/thread/32b0...
Reply all
Reply to author
Forward
0 new messages