IMServer:MongoDB: Difference between revisions
Line 8: | Line 8: | ||
== Creating indexes == | == Creating indexes == | ||
<source lang="python"> | |||
import pymongo | import pymongo | ||
Line 33: | Line 35: | ||
col.ensure_index("name") | col.ensure_index("name") | ||
print "\r Done ... " | print "\r Done ... " | ||
</source> | |||
== Cookbook == | == Cookbook == |
Revision as of 16:26, 19 January 2011
MongoDB
- 64 bit recommended due to 2gb data size barrier in 32bit
- 1.7.4 has some enhancements like 16mb limit per binary record
- Binary can be downloaded from
- On deployment server, needs a script to be installed in init.d to stop / restart / start mongodb
Creating indexes
<source lang="python">
import pymongo
- connect with the database
try: conn = pymongo.Connection() db = conn['clay1'] print 'Connection OK' except: print 'Cound not Connect ..'
- loop through all collections
cols = db.collection_names()
for col_name in cols: # skip if starts with system if col_name[0:6] <> 'system': print 'Opening collection : ' + col_name col = db[col_name] print ' With %d records'%(col.count()) print ' ' + str(col.index_information()) print ' Creating index ...' col.ensure_index("name") print "\r Done ... " </source>
Cookbook
How to find if a collection exists
Also it helps if mongo can be queried for existing collections first. That can be done by querying system.namespaces collection in that database, which lists all the collections
<source lang="javascript" show dbs use clay1 db.system.namespaces.find({ "name" :"clay1.n5"}) >
"clay1" is the name of the database, and "n5" is the name of the collection to find