Question

My app is a korean-english dictionary that I am writing on node.js using MongoDb. The problem is to organize database so I can have korean-english and english-korean direction with the same words. In MySQL I would use table of all words and many-to-many connection table with words references.
For example I have
A is C
A is D
B is C
Translations for A are C, D - one direction
Translations for C are A, B - another direction
How do I organize database in Mongo?

Was it helpful?

Solution

if i understood you right.

assuming you have this collection.

db.collection({'en': 'word_en', 'kr':'word_kr'})

if you want to search en eqvivalent of 'korean' word just write:

db.collection.find({'kr': 'word'})

and vice versa

db.collection.find({'en': 'word'})

do not forget create indexes for collection.

Also, make sure that your indices aren't unique, so you can be able to add multiple references. So in extension of your example, you would get something like this:

db.collection({'en': 'A', 'kr':'C'})
db.collection({'en': 'A', 'kr':'D'})
db.collection({'en': 'B', 'kr':'C'})

And when you want translations for A:

db.collection.find({'en': 'A'})

you get an array with C and D. Similar with moving from C

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top