TroyGrosfield.com TroyGrosfield.com

Headline

MongoDB Cheat Sheet

Author
by Troy Grosfield
Date
February 22nd, 2012
Category
Developer
Story

A few commands that help with querying and troubleshooting mongodb. This document assumes we have a database called “test” and we have a collection called “comments”.

Commands

Number of documents in a collection:

$ db.comments.count()
42232

Show the size of the Index:

$ db.comments.totalIndexSize();
73696

Show details of a specific query:

$ db.comments.find({ tags : 'mongodb'})
             .sort({ created_at : -1 }).explain();
{
	"cursor" : "BasicCursor",
	"nscanned" : 14,
	"nscannedObjects" : 14,
	"n" : 1,
	"millis" : 0,
	"nYields" : 0,
	"nChunkSkips" : 0,
	"isMultiKey" : false,
	"indexOnly" : false,
	"indexBounds" : {

	}
}

Get db stats:

$ db.stats()
{
	"db" : "test",
	"collections" : 24,
	"objects" : 1126,
	"avgObjSize" : 2726.8490230905863,
	"dataSize" : 3070432,
	"storageSize" : 16617728,
	"numExtents" : 41,
	"indexes" : 78,
	"indexSize" : 1007280,
	"fileSize" : 201326592,
	"nsSizeMB" : 16,
	"ok" : 1
}

Get collection stats:

$ db.comments.stats()
{
	"ns" : "test.comments",
	"count" : 14,
	"size" : 218804,
	"avgObjSize" : 15628.857142857143,
	"storageSize" : 520192,
	"numExtents" : 2,
	"nindexes" : 2,
	"lastExtentSize" : 480512,
	"paddingFactor" : 1.0099999999999927,
	"flags" : 1,
	"totalIndexSize" : 24576,
	"indexSizes" : {
		"_id_" : 8192,
		"test_by_comments" : 8192,
		"users_by_date" : 8192,
	},
	"ok" : 1
}

Resources

Tags
Comments
1 Comment »

One Comment

Leave a reply

 
  1. Author
    shiv kumar suman
    Date
    May 28th, 2012 at 1:18 am
    Comment

    good but i want all about unix ie complete portion of unix.