Reputation: 1823
I need to query for data contained in embedded objects. The problem is that embedded objects has unknown structure.
Example:
{"field_1": "val_1", "embedded": {"random1": "someA" }}
{"field_1": "val_2", "embedded": {"random2": "someB" }}
{"field_1": "val_3", "embedded": {"random3": "someC" }}
I need to be able to get 2nd document when searching for "someB"
Any ideas how can I search in embedded documnet that have unknown structure?
ps for hardcoded objects I use regex query:
{'$regex': '.*%s.*' % search_for}
Upvotes: 1
Views: 357
Reputation:
You can not apply regular expressions to schema fields. You always have to specify the full dotted name of an document/subdocument to query. Otherwise you need to aggregate your stuff to be searched into a well-known field that can be queried. But MongoDB is not a solution for finding all-my-crap-stored-within-my-horrible-crap-data-schema.
Upvotes: 0
Reputation: 230286
How are you going to query unknown structure? You might be taking schemaless approach too far.
You probably want to create and maintain reverse indexes by yourself. That is, in separate collection store documents like this:
{value: 'someB', stored_in_object:"object id here", path:'embedded.random2'}
Or maybe full-text search engines can help here.
Or you should rethink your schema.
Upvotes: 2