Skip to content

Commit

Permalink
Added client, db, and name variables to MongoCollection.
Browse files Browse the repository at this point in the history
Tests have been added to check for assignment, and the documentation mentions variables availability.
  • Loading branch information
ivirshup committed Feb 26, 2016
1 parent 9c07214 commit e4deb90
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ using Mongo, LibBSON
# Create a client connection
client = MongoClient() # default locahost:27017

# Get a handle to collection named "cats" in database "db"
# Get a handle to collection named "cats" in database "db".
# Client object, database name, and collection name are stored as variables.
cats = MongoCollection(client, "db", "cats")

# Insert a document
Expand Down
8 changes: 7 additions & 1 deletion src/MongoCollection.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
type MongoCollection
_wrap_::Ptr{Void}
client::MongoClient
db::AbstractString
name::AbstractString

MongoCollection(client::MongoClient, db::AbstractString, name::AbstractString) = begin
dbCStr = bytestring(db)
Expand All @@ -9,7 +12,10 @@ type MongoCollection
(:mongoc_client_get_collection, libmongoc),
Ptr{Void}, (Ptr{Void}, Ptr{UInt8}, Ptr{UInt8}),
client._wrap_, dbCStr, nameCStr
)
),
client,
db,
name
)
finalizer(collection, destroy)
return collection
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ facts("Mongo") do
collection = MongoCollection(client, "foo", "bar")
oid = BSONOID()

context("variables") do
@fact collection.client --> client
@fact collection.db --> "foo"
@fact collection.name --> "bar"
end

context("insert") do
insert(collection, ("_id" => oid, "hello" => "before"))
@fact count(collection, ("_id" => oid)) --> 1
Expand Down

0 comments on commit e4deb90

Please sign in to comment.