alste
alste

Reputation: 1455

MongoDB database design with embedded documents

I have a few thousand strings (items) that I would like to translate. I have structured my MongoDB as follows:

@document = {:item => "hello", :translations => {:fr => {:name => "bonjour", 
:note => "easy"}, :es => {:name => "hola", :note => "facil"}}}

The :translations field can contain many more languages and properties. I would like to run queries such as retrieving all items with no translations for a specific language, or retrieving all items having 'bonjour' as a French translation.

I don't see how I can do this. Is there a better way to structure my database for these purposes? I am using node.js.

Thanks.

Upvotes: 1

Views: 364

Answers (2)

Gates VP
Gates VP

Reputation: 45287

I would like to run queries such as retrieving all items with no translations for a specific language,

.find({ 'translations.fr': {$exists:false} })

...or retrieving all items having 'bonjour' as a French translation.

.find({ 'translations.fr.name': "bonjour" })

Is there a better way to structure my database for these purposes?

I believe that you have the correct structure. You will have to become familiar with the Dot Notation.

Upvotes: 3

Madd0g
Madd0g

Reputation: 4001

I'd say that for your purpose the model is good. You need mongo dot notation, you can use $exists to look for fr and dot notation for bonjour -

find({ "fr.name" : "bonjour" })

Upvotes: 1

Related Questions