Details
-
New Feature
-
Resolution: Won't Fix
-
Major - P3
-
None
-
None
-
None
-
Query
Description
What I'm about to suggest could be (imho) a game changer about how we (developers) use databases.
Please note that this feature maybe won't apply/benefit 100% of the cases, but it will surely benefit the vast majority.
What I'm proposing is a way to save different data in a single field and make the database engine return one value or another depending on the scope of the query. Let me explain further...
Translations are really common in web-development. A single product's description (in an e-commerce website) has different translations in multiple languages (english, spanish, italian, french, etc...). It doesn't matter if we're using SQL or NoSQL, we're used to create as many fields as languages we need to support (desc_en, desc_es, desc_it, desc_fr, etc...) or even worse, different tables/documents.
But what if we could store all those different translations in the same field and make MongoDB return a specific translation or if that one is missing, return a "default" one?
Something like:
db.collections.products.find({
|
_id: 123456789,
|
$scope: {
|
try: "spanish",
|
catch: "english"
|
}
|
}, { product_name: 1, product_desc: 1 });
|
That will query the database for the name and the description of the product with _id 12346789, first trying to get the spanish translation of those (scoped) fields, and if any of those fields doesn't have a spanish translation, it will fetch the english one. That way we could end up with a result like this:
{
|
name: "T-Shirt",
|
desc: "Camiseta de verano, 100% algodon"
|
}
|
The same way we could insert different translations in a single field using that same syntax (setting the "scope" of the field).
This could be used in translations (as my example shows), different metric units (car top speed -> km/h vs mph), different data representation (value of color -> human vs hex vs rgba), etc...
The possibilities are endless, and it will improve a lot how we currently deal with those kind of problems.
As an extra, it would be nice to "query the scopes" of a document, so we can know that the document containing a product does actually contain the english, spanish, italian and french "scopes" (translations).