Skip to content

Instantly share code, notes, and snippets.

@neomantra
Created August 4, 2011 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neomantra/1125619 to your computer and use it in GitHub Desktop.
Save neomantra/1125619 to your computer and use it in GitHub Desktop.
mongol API sketch
-- mongol is based on the mongo C driver:
-- http://api.mongodb.org/c/current/
-- BSON types are marshalled just like luamongo:
-- http://code.google.com/p/luamongo/wiki/BsonTypes
-- new empty BSON object
b = bson.new()
-- new BSON object
-- key order is the same as pairs(table)
-- integer keys are stored as tostring(n), this may not be what you want
b = bson.new( { k = 'v', [1] = 4, [3] = bson.NumberInt(2) } )
-- new BSON object
-- key order is specified in the second argument
-- note in this example, 3 will not be included!
b = bson.new( { k = 'v', [1] = 4, [3] = bson.NumberInt(2) }, {1, 'k'} )
-- new BSON object
-- key/value passed as arguments (rather than table)
-- note that all keys arguments must be a number or string
-- the intelligence in this is left to the caller
b = bson.new( k1, v1, k2, v2, ... )
-- you can build an BSON object as well
c = bson.build( 32 ) -- optionally gives an initial buffer size in bytes
c:append( k1, v2 )
c:append( k1, v2 )
b = c:finish()
-- you can clone objects too
b2 = b:copy()
-- now let's iterate these objects
for k, v in b:pairs() do
print( k, v )
end
-- or directly look for a value at a key
v = b["color"] -- if not found, will be nil
--- mongo stuff
-- connect to a mongodb server
db = mongo.connect( "127.0.0.1", 27017 )
-- connect to a mongodb replica set
db = mongo.replset( "shard1", { "10.4.3.22:27017", "10.4.3.22:27018" } )
db:connect()
-- alternatively add seeds individually
db = mongo.replset( "shard1" )
db:add_seed( "10.4.3.22", 27017 )
db:add_seed( "10.4.3.22", 27018 )
db:connect()
-- insertion
db:insert( "db.coll", b ) -- some previously created bson object
db:insert( "db.coll", bson.new{ k = 2 } )
-- batch insertion
db:insert( "db.coll", { b1, b2, b3, b4 } )
-- queries
curr = db:find( "db.coll" )
for k, v in pairs(cur) do
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment