IMServer:MongoDB: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[Category:Connectome]]
== MongoDB ==
== MongoDB ==


Line 8: Line 11:


== Creating indexes ==
== Creating indexes ==
Following is a python script which loops through all the collections of a database and ensure index over field "name"


<pre lang="python">
<pre lang="python">
import pymongo
import pymongo



Latest revision as of 18:10, 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

Following is a python script which loops through all the collections of a database and ensure index over field "name"

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

Following is the sequence of commands to be typed in mongo command line client to find out whether a named collection exists. That can be done by querying system.namespaces collection in that database, which lists all the collections

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