IMServer:MongoDB: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
Line 40: Line 40:


=== How to find if a collection exists ===  
=== 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
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


<source lang="javascript">
<pre lang="javascript">
show dbs
show dbs
use clay1
use clay1
db.system.namespaces.find({ "name" :"clay1.n5"})
db.system.namespaces.find({ "name" :"clay1.n5"})
</source>
</pre>


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

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

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