IMServer:MongoDB: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
Line 9: Line 9:
== Creating indexes ==
== Creating indexes ==


<source lang="python">
<pre lang="python">


import pymongo
import pymongo
Line 35: Line 35:
col.ensure_index("name")
col.ensure_index("name")
print "\r  Done ...    "
print "\r  Done ...    "
</source>
</pre>


== Cookbook ==
== Cookbook ==

Revision as of 16:31, 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


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 ...    "

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"}) </source>

"clay1" is the name of the database, and "n5" is the name of the collection to find