mongo:createObject

coronium.mongo:createObject( collection, data_table )

Saves the data_table to MongoDB under the supplied collection.

Object IDs

createObject method will also create an objectId key with a unique object id that you can use in other methods.

Parameters

Name Details
collection The collection name to add this object to. (String, required)
data_table The data record to add to the collection. (Table, required)

Returns

Examples

local answer = coronium.mongo:createObject( "users",
{ username = "John" } )
if not answer.error then
  print( answer.result.objectId )
end

mongo:getObject

coronium.mongo:getObject( collection, objectId )

Returns a data table from MongoDB based on the supplied objectId

Parameters

Name Details
collection The collection name to access. (String, required)
objectId The record object id in the collection. (String, required)

Returns

Examples

local data_table
local answer = coronium.mongo:getObject( "users", "abcd1234" )
if not answer.error then
  data_table = answer.result
end

mongo:findObject

coronium.mongo:findObject( collection, query_table, show_keys )

Returns a data table from MongoDB based on the supplied query. Optional shown key flags

Parameters

Name Details
collection The collection to perform the find with. (String, required)
query_table The query table used to filter the results. (Table, required)
show_keys Select specific keys you would like to retrieve. (Table, optional)

Returns

Examples

local answer = coronium.mongo:findObject( "users",
{ username = "Smith" }, { age = 0 } )
if not answer.error then
  print( answer.result.username )
end

mongo:getObjects

coronium.mongo:getObjects( collection, query_tbl, limit, sortField, sortOrder )

Searches a collection based on the provided parameters. The 'query_tbl' is a table with keys you'd like to search against.

Parameters

Name Details
collection The collection to get objects from. (String, required)
query_tbl The query table to filter objects with. (Table, required)
limit The amount of results requested. (Number, optional)
sortField The object field to sort on. (String, optional)
sortOrder The sort order for the result set. Can be ASC or DESC. (Constant, optional)

Returns

Examples

local data_table
local answer = coronium.mongo:getObjects( "users",
{ points = { ["$gt"] = 1000 } )
if not answer.error then
  data_table = answer.result
end

Query "dogs" with age and color criteria, return up to 100 records (default)

local answer = coronium.mongo:getObjects( "dogs", { age = 12, color = "Black" } )

Get any "dogs" record, up to 200 results.

local answer = coronium.mongo:getObjects( "dogs", {}, 200 )

Get up to 50 records and sort by "age" descending.

local answer = coronium.mongo:getObjects( "dogs", {}, 50, "age", "DESC" )

Search Tips

You can use special operators for searching: $lt - less than | $lte - less or equal | $gt - greater than | $gte - greater or equal | $ne - not equal, useful for testing empties.

-- example 1: users named 'Lori'
local answer = coronium.mongo:getObjects( "users",
{ username = "Lori" } )
-- example 2: all scores less than 1000
local answer = coronium.mongo:getObjects( "scores",
{ score = { ["$lt"] = 1000 } } )
-- example 3: user is admin and score is greater or equal to 2500
local answer = coronium.mongo:getObjects( "users",
{ admin = true, score = { ["$gte"] = 2500 } } )
-- get result
if not answer.error then
  print( #answer.result )
end

mongo:updateObject

coronium.mongo:updateObject( collection, objectId, data_table )

Updates a stored object with the supplied objectId and updated data table.

Parameters

Name Details
collection The collection name to reference. (String, required)
objectId The objectId of the object to update. (Table, required)
data_table The keys to update on the record. (Table, required)

Returns

Examples

local answer = coronium.mongo:updateObject( "users", "abcd1234",
{ admin = true } )
if not answer.error then
  print( "updated" )
end

mongo:updateObjects

coronium.mongo:updateObjects( collection, query, update_table )

Updates stored objects with the supplied query and updated data table.

Parameters

Name Details
collection The collection name to reference. (String, required)
query A query table to filter update objects. (Table, required)
update_table The keys to inc/dec on including amounts. (Table, required)

Returns

Examples

--Set all users whose boots are leather and hunger is satiated to a ready state.
local answer = coronium.mongo:updateObjects( "users",
{ boots = "Leather", hunger = 100 }, { ready = true } )
if not answer.error then
  print( "updated user objects" )
end

mongo:incObjectKeys

coronium.mongo:incObjectKeys( collection, objectId, inc_data_table )

Updates the keys numerical count by N for Object belonging to objectId.

Parameters

Name Details
collection The collection name to reference. (String, required)
objectId The objectId of the object to inc/dec. (String, required)
inc_data_table The keys to inc/dec on including amounts. (Table, required)

Returns

Examples

-- Increments the score 12000 points, must be number type.
local answer = coronium.mongo:incObjectKeys( "users", "abcd1234",
{ score : 12000 } )
if not answer.error then
  print( "Score adjusted." )
end

-- Removes 25 Karma points, Adds 20 warning, must be number type.
local answer = coronium.mongo:incObjectKeys( "users", "abcd1234",
{ karma : -25, warning: 20 } )
if not answer.error then
  print( "Karma and warning adjusted." )
end

mongo:removeObjectKeys

coronium.mongo:removeObjectKeys( collection, objectId, keys_to_remove_table )

Removes objectId keys and associated data from the provided table array of keys.

Parameters

Name Details
collection The collection name to reference. (String, required)
objectId The objectId of the object to remove keys from. (String, required)
keys_to_remove_table A table array of the keys to remove from the object. (Table, required)

Returns

Examples

local data_table
--Removes the age and height key for this users data object
local answer = coronium.mongo:removeObjectKeys( "users", "abcd1234",
{ "age", "height" } )
if not answer.error then
  data_table = answer.result
end

mongo:deleteObject

coronium.mongo:deleteObject( collection, objectId )

Deletes an object from MongoDB based on the supplied objectId.

Parameters

Name Details
collection The collection name to reference. (String, required)
objectId The objectId of the object to delete. (String, required)

Returns

Examples

local answer = coronium.mongo:deleteObject( "users", "abcd1234" )
if not answer.error then
  print( "deleted" )
end

mongo:deleteObjects

coronium.mongo:deleteObjects( collection, query )

Deletes objects from MongoDB based on the supplied query.

Parameters

Name Details
collection The collection name to reference. (String, required)
query The query table to filter results. (Table, required)

Returns

Examples

local answer = coronium.mongo:deleteObjects( "users", { canceled : true } )
if not answer.error then
  print( "deleted canceled objects" )
end

mongo:getObjectCount

coronium.mongo:getObjectCount( collection, query_table )

Counts the records in collection based on the provided query_table.

Parameters

Name Details
collection The collection name to count on. (String, required)
query_table The query table to filter results. (Table, optional)

Returns

Examples

Video Tutorial

Video Tutorial on YouTube