   name: 'mongo'
 
   create: (cName, docName, data, callback) ->
-    data._id = docName
-    mongo.collection(cName).insert data, callback
+    return callback 'db already closed' if @closed
+    doc = castToDoc docName, data
+    mongo.collection(cName).insert doc, callback
 
   close: ->
-    throw new Error 'db already closed' if @closed
+    return callback 'db already closed' if @closed
     mongo.close()
     @closed = true
 
   getSnapshot: (cName, docName, callback) ->
-    throw new Error 'db already closed' if @closed
-    mongo.collection(cName).findOne {_id:docName}, (err, data) ->
-      delete data._id if data
-      callback err, data
+    return callback 'db already closed' if @closed
+    mongo.collection(cName).findOne {_id:docName}, (err, doc) ->
+      callback err, castToSnapshot(doc)
 
   setSnapshot: (cName, docName, data, callback) ->
-    throw new Error 'db already closed' if @closed
-    data.data = null if data.data is undefined
-    data.type = null if data.type is undefined
-    mongo.collection(cName).update {_id:docName}, {$set:data}, {upsert:true}, callback
+    return callback 'db already closed' if @closed
+    doc = castToDoc docName, data
+    mongo.collection(cName).update {_id:docName}, doc, {upsert:true}, callback
 
   query: (cName, inputQuery, callback) ->
     return callback 'db already closed' if @closed

    else
      query.$query._id = docName

    mongo.collection(cName).findOne query, (err, result) ->
      if result
        result.docName = docName
        delete result._id
      callback err, result

  # Test whether an operation will make the document its applied to match the specified query.
  # This function doesn't really have enough information to know in all cases, but if we can determine

        cursor[item[0]] item[1]

      cursor.toArray (err, results) ->
        results = results && results.map(castToSnapshot)
        callback err, results

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

    if query.$query._id
