Dan
Dan

Reputation: 3459

Query to return only part of a value

Is there a way to return a part of a value in a collection in a manner similar to a capturing group in a regular expression? For instance, if I have values like:

MyKey: "PrefixDataIDontCareAbout-DataIDoCareAbout-OtherDataIDontCareAbout"

Is there a way to return just the DataIDoCareAbout portion using regex or some other method? Substrings won't work because the data I need to extract is more complex than just a fixed range of characters. And this would need to be done at the query level because I need to group them as distinct so the same value will only show once.

Any tips would be great!

Upvotes: 0

Views: 200

Answers (3)

Sammaye
Sammaye

Reputation: 43884

Not in regex style no.

There is only the $substr command built into the aggregation framework that would be of any use here however this only runs over integer position of characters within a string.

I also would not regex this depending upon your queries. If you limit the query by other fields then it should be ok, i.e. user_id and regex however using a regex, in none-prefixed format as you would have to, could be very slow.

Upvotes: 0

mrówa
mrówa

Reputation: 5771

One solution would be to create javascript script that would run multiple queries: first that would be able to acquire the "DataIDoCareAbout" part and next, that would run distinct and other stuff you really need using the results.

Upvotes: 0

Pablo Fernandez heelhook
Pablo Fernandez heelhook

Reputation: 12503

No, and largely requiring to do that signals a problem in your data model, you would be better served to refactor your model to leave DataIDoCareAbout as a separate, perhaps indexable, key.

Upvotes: 1

Related Questions