Skip to content

Instantly share code, notes, and snippets.

@tc
Created October 6, 2010 00:09
Show Gist options
  • Save tc/612584 to your computer and use it in GitHub Desktop.
Save tc/612584 to your computer and use it in GitHub Desktop.
import com.mongodb.{Mongo, DB, DBObject}
import com.mongodb.util.JSON
import org.junit.{Assert, Test}
/**
*
* User: @tommychheng
* Date: Sep 24, 2010
* Time: 9:36:41 AM
*
*
*/
class MongoTest{
@Test
def testInsertJSQuery{
val m = new Mongo()
val db = m.getDB("test")
val collection = db.getCollection("test_data")
collection.drop() //make sure the collection is empty
val document = """ {"foo": "bar", "age": 5} """
val documentObject = JSON.parse(document).asInstanceOf[DBObject]
collection.insert(documentObject)
val query = """ {"foo": "bar"} """
val queryObject = JSON.parse(query).asInstanceOf[DBObject]
val cursor = collection.find(queryObject).iterator
while(cursor.hasNext){
val item = cursor.next
Assert.assertEquals("bar", item.get("foo"))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment