   # 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
+  query.$query._type = {$ne: null} unless query.$query._type
 
   return query
+
+castToDoc = (docName, data) ->
+  doc = if data.type is 'http://sharejs.org/types/JSONv0'
+    data.data || {}
+  else
+    data: if data.data? then data.data else null
+  doc._type = data.type || null
+  doc._v = data.v
+  doc._id = docName
+  return doc
+
+castToSnapshot = (doc) ->
+  return unless doc
+  type = doc._type
+  v = doc._v
+  docName = doc._id
+  delete doc._type
+  delete doc._v
+  delete doc._id
+  if type is 'http://sharejs.org/types/JSONv0'
+    data =
+      data: doc
+      type: type
+      v: v
+      docName: docName
+  else
+    data = doc
+    data.type = type
+    data.v = v
+    data.docName = docName
+  return data
 
 # mongo is a mongoskin client. Create with:
 #  mongo.db('localhost:27017/tx?auto_reconnect', safe:true)

  name: 'mongo'

  create: (cName, docName, data, callback) ->
    data._id = docName
    mongo.collection(cName).insert data, callback

  close: ->
    throw new Error '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

  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

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

    else
      query.$query._id = docName

    mongo.collection(cName).findOne query, (err, doc) ->
      callback err, castToSnapshot(doc)

  # 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
