[SERVER-4633] Retrieve only the first matched object Created: 06/Jan/12 Updated: 06/Dec/22 Resolved: 04/Dec/17 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Querying |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | New Feature | Priority: | Minor - P4 |
| Reporter: | Rodrigo Coelho | Assignee: | Backlog - Query Team (Inactive) |
| Resolution: | Done | Votes: | 0 |
| Labels: | conditional_operator, query | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Assigned Teams: |
Query
|
| Backwards Compatibility: | Fully Compatible |
| Participants: |
| Description |
|
This is a proposal for a new feature that makes it possible to limit a result to only one object. The use case: multilanguage documents with support for fallback to a default language. For this document:
I'd like to query conditionally for the user (website visitor) language, with a fallback to the default (app) language, but returning only one of them. Suppose user's language is 'fr' and app default language is 'it'.
Please note that the query features a (hopefully) future feature specified in Expected result:
I hope that this can be implemented without issues with indexes, parallel processing and sharded setups. |
| Comments |
| Comment by Asya Kamsky [ 04/Dec/17 ] | ||||||||||||
|
Here's the code to do it given sample document schema:
Replace "fr" string with string representing preferred language. | ||||||||||||
| Comment by Asya Kamsky [ 04/Dec/17 ] | ||||||||||||
|
This can already be achieved with Aggregation framework $project or $addFields using expressions like $cond or $filter. Also related to this is SERVER-17038 which is trying to use $redact, but with objects stored in flat array $redact is not necessary and it can be done with $filter. | ||||||||||||
| Comment by Rodrigo Coelho [ 06/Jan/12 ] | ||||||||||||
|
For more flexibility for use cases that require more than one result, $inFirst and $orFirst could have an optional parameter specifying how many objects to match. |