 				top.v_end = d.v
 				top.meta.end_ts = d.meta.end_ts
 				return
-			else
+			else if sz < MAX_SIZE
 				# create a new pack
 				top = _.clone(d)
 				top.pack = [ {v: d.v, meta: d.meta,  op: d.op, _id: d._id} ]

_ = require 'underscore'
MAX_SIZE = 1024*1024
MAX_COUNT = 1024

packOps = (doc_id, callback) ->
	console.log 'packing', doc_id
	db.docHistory.find({doc_id:mongojs.ObjectId(doc_id),pack:{$exists:false}}).sort {v:1}, (err, docs) ->
		packs = []
		top = null
		if docs.length < 100
			console.log 'only', docs.length, 'ops, skipping'
			return
		docs.forEach (d,i) ->
			sz = BSON.calculateObjectSize(d)
			if top?	&& top.pack.length < MAX_COUNT && top.sz + sz < MAX_SIZE

				delete top.op
				delete top._id
				packs.push top
			else
				# keep the op
				console.log 'keeping large op unchanged'

		# only store packs with a sufficient number of ops, discard others
		packs = packs.filter (packObj) ->
			packObj.pack.length > MIN_COUNT

		console.log 'docs', origDocs, 'packs', packs.length
		async.each packs, insertPack, (err, result) ->
			if err?
				console.log 'err', err
			console.log 'done writing packs'
			callback err, result

async.each process.argv.slice(2),	(doc_id, callback) ->
		packDocHistory(doc_id, callback)
	, (err, results) ->
		console.log 'closing db'
		db.close()
