Using MongoDB as a persistent Hashtable

Go To StackoverFlow.com

1

note: I am sorry if this is a stupid question!

I have this program that generates data that it stores in pythons dictionaries.

Since this is getting bigger and bigger, and since this is not persistent and the program has to create them all over again, each time it runs....

I tried to use mongodb, but it seems to need both key and value to search(find function). But I have only the key, and I need the value.

How is this done?

2012-04-05 16:49
by Vignesh


1

Store each dictionary entry (key, value pair) as a separate document in mongo {key: "keyval", value: "valueval"} and provide only the key to the find function:

mydocs.find({'key': something}, fields=['value'])

If the collection is large, you'll need to add an index on key:

mydocs.ensure_index('key')
2012-04-05 17:37
by georg
Hi, thanks, i thought of that one too... It just seemed to add complexity... But I implemented it this way. And thanks for the extra tip ensure_index too - Vignesh 2012-04-06 11:46
for those not in python, the correct use within the mongodb shell can be found here: http://docs.mongodb.org/manual/reference/method/db.collection.ensureIndex/, mainly you need db.collectionName.ensureIndex({"key" : 1});lollercoaster 2013-06-27 15:21
Ads