           throw e
 
 
-  queryDoc: (cName, docName, query, callback) ->
-    query = normalizeQuery query
+  queryDoc: (cName, docName, inputQuery, callback) ->
+    query = normalizeQuery inputQuery
 
     if query.$query._id
       return callback() if query.$query._id isnt docName

  out[key] = object[key] for key of object
  return out

normalizeQuery = (query) ->
  # Box queries inside of a $query and clone so that we know where to look
  # for selctors and can modify them without affecting the original object
  if query.$query
    query = shallowClone query
    query.$query = shallowClone query.$query
  else
    query = $query: shallowClone(query)

  # Deleted documents are kept around so that we can start their version from
  # the last version if they get recreated. When they are deleted, their type
  # is set to null, so don't return any documents with a null type.
  query.$query.type = {$ne: null} unless query.$query.type
 
  return query

# mongo is a mongoskin client. Create with:

    data.type = null if data.type is undefined
    mongo.collection(cName).update {_id:docName}, {$set:data}, {upsert:true}, callback

  query: (cName, inputQuery, callback) ->
    return callback 'db already closed' if @closed

    cursorMethods = extractCursorMethods inputQuery

    query = normalizeQuery inputQuery

    mongo.collection(cName).find query, (err, cursor) ->
      return callback err if err

      for item in cursorMethods
        cursor[item[0]] item[1]

      cursor.toArray (err, results) ->
        if results then for r in results
