README 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. NDB_MONGODB Module
  2. Daniel-Constantin Mierla
  3. <[email protected]>
  4. Edited by
  5. Daniel-Constantin Mierla
  6. <[email protected]>
  7. Copyright © 2014 asipto.com
  8. __________________________________________________________________
  9. Table of Contents
  10. 1. Admin Guide
  11. 1. Overview
  12. 2. Dependencies
  13. 2.1. Kamailio Modules
  14. 2.2. External Libraries or Applications
  15. 3. Parameters
  16. 3.1. server (str)
  17. 4. Functions
  18. 4.1. mongodb_cmd(srvname, dbname, cname, command, replyid)
  19. 4.2. mongodb_cmd(srvname, dbname, cname, command, replyid)
  20. 4.3. mongodb_find(srvname, dbname, cname, command, replyid)
  21. 4.4. mongodb_next(replyid)
  22. 4.5. mongodb_free(replyid)
  23. List of Examples
  24. 1.1. Set server parameter
  25. 1.2. mongodb_cmd usage
  26. 1.3. mongodb_cmd_simple usage
  27. 1.4. mongodb_find usage
  28. 1.5. mongodb_next usage
  29. 1.6. mongodb_free usage
  30. Chapter 1. Admin Guide
  31. Table of Contents
  32. 1. Overview
  33. 2. Dependencies
  34. 2.1. Kamailio Modules
  35. 2.2. External Libraries or Applications
  36. 3. Parameters
  37. 3.1. server (str)
  38. 4. Functions
  39. 4.1. mongodb_cmd(srvname, dbname, cname, command, replyid)
  40. 4.2. mongodb_cmd(srvname, dbname, cname, command, replyid)
  41. 4.3. mongodb_find(srvname, dbname, cname, command, replyid)
  42. 4.4. mongodb_next(replyid)
  43. 4.5. mongodb_free(replyid)
  44. 1. Overview
  45. This module provides a non-db connector to interact with MongoDB NoSQL
  46. server from configuration file. You can read more about MongoDB at
  47. http://www.mongodb.org.
  48. It can connect to many MongoDB servers and store the results in
  49. different containers.
  50. 2. Dependencies
  51. 2.1. Kamailio Modules
  52. 2.2. External Libraries or Applications
  53. 2.1. Kamailio Modules
  54. The following modules must be loaded before this module:
  55. * none.
  56. 2.2. External Libraries or Applications
  57. The following libraries or applications must be installed before
  58. running Kamailio with this module loaded:
  59. * mongo-c-driver - available at
  60. https://github.com/mongodb/mongo-c-driver
  61. 3. Parameters
  62. 3.1. server (str)
  63. 3.1. server (str)
  64. Specify the details to connect to REDIS server. It takes a list of
  65. attribute=value separated by semicolon, the attributes can be name and
  66. uri. Name is a generic identifier to be used with module functions. The
  67. uri parameter must be a valid MongoDB database connection string.
  68. You can set this parameter many times, in case you want to connect to
  69. many MongoDB servers, just give different attributes and use the
  70. specific server name when querying the MongoDB instance.
  71. Default value is NULL.
  72. Example 1.1. Set server parameter
  73. ...
  74. modparam("ndb_mongodb", "server", "name=mgs1;uri='mongodb://localhost/kamailio'"
  75. )
  76. modparam("ndb_mongodb", "server", "name=mgs2;uri='mongodb://127.0.0.2/kamailio'"
  77. )
  78. ...
  79. 4. Functions
  80. 4.1. mongodb_cmd(srvname, dbname, cname, command, replyid)
  81. 4.2. mongodb_cmd(srvname, dbname, cname, command, replyid)
  82. 4.3. mongodb_find(srvname, dbname, cname, command, replyid)
  83. 4.4. mongodb_next(replyid)
  84. 4.5. mongodb_free(replyid)
  85. 4.1. mongodb_cmd(srvname, dbname, cname, command, replyid)
  86. Send a valid command to MongoDB server identified by srvname. The reply
  87. will be stored in a local container identified by replyid. All the
  88. parameters can be strings with pseudo-variables that are evaluated at
  89. runtime.
  90. The reply can be accessed via pseudo-variable $mongodb(key). The key
  91. can be: type - type of the reply; value - the value in JSON format
  92. returned by MongoDB server; info - in case of error from MongoDB, it
  93. will contain an info message.
  94. The result can contain many documents, see mongodb_next() function for
  95. more details.
  96. Meaning of the parameters:
  97. * srvname - MongoDB server name as given via 'server' parameter.
  98. * dbname - MongoDB database name.
  99. * cname - MongodDB collection (table) name.
  100. * command - valid MongoDB command given as JSON string.
  101. * replyid - the name of the container to store the response.
  102. The function can be used from ANY_ROUTE.
  103. Example 1.2. mongodb_cmd usage
  104. ...
  105. if(mongodb_cmd("mgs1", "kamailio", "acc", "{ \"collStats\": \"acc\" }", "mgr1"))
  106. {
  107. xlog("response from mongodb is [[$mongodb(mgr1=>value)]]\n");
  108. }
  109. ...
  110. 4.2. mongodb_cmd(srvname, dbname, cname, command, replyid)
  111. Send a valid command to MongoDB server identified by srvname. The reply
  112. will be stored in a local container identified by replyid. All the
  113. parameters can be strings with pseudo-variables that are evaluated at
  114. runtime.
  115. The reply can be accessed via pseudo-variable $mongodb(key). The key
  116. can be: type - type of the reply; value - the value in JSON format
  117. returned by MongoDB server; info - in case of error from MongoDB, it
  118. will contain an info message.
  119. This function should be used when the response has only one document.
  120. See mongodb_cmd() for the meaning of the parameters.
  121. The function can be used from ANY_ROUTE.
  122. Example 1.3. mongodb_cmd_simple usage
  123. ...
  124. if(mongodb_cmd_simple("mgs1", "kamailio", "acc", "{ \"collStats\": \"acc\" }", "
  125. mgr1")) {
  126. xlog("response from mongodb is [[$mongodb(mgr1=>value)]]\n");
  127. }
  128. ...
  129. 4.3. mongodb_find(srvname, dbname, cname, command, replyid)
  130. Send a find command to MongoDB server identified by srvname. The reply
  131. will be stored in a local container identified by replyid. All the
  132. parameters can be strings with pseudo-variables that are evaluated at
  133. runtime.
  134. The reply can be accessed via pseudo-variable $mongodb(key). The key
  135. can be: type - type of the reply; value - the value in JSON format
  136. returned by MongoDB server; info - in case of error from MongoDB, it
  137. will contain an info message.
  138. This function is useful for querying collections and accessing the
  139. results.
  140. See mongodb_cmd() for the meaning of the parameters, with the remark
  141. that command has to be a valid filter JSON document, like for find()
  142. command in mongoc command line client tool.
  143. The function can be used from ANY_ROUTE.
  144. Example 1.4. mongodb_find usage
  145. ...
  146. if(mongodb_find("mgs1", "kamailio", "acc", "{ \"src_user\" : \"111\" }", "mgr1")
  147. ) {
  148. xlog("response from mongodb is [[$mongodb(mgr1=>value)]]\n");
  149. }
  150. ...
  151. 4.4. mongodb_next(replyid)
  152. Moves to next document in a MongoDB reply. This function can be used
  153. after a succesful mongodb_cmd() or mongodb_find(). It returns true if
  154. there is a shift to document. The current document becomes available
  155. via $mongodb(key) variable.
  156. Example 1.5. mongodb_next usage
  157. ...
  158. if(mongodb_find("mgs1", "kamailio", "acc", "{ \"src_user\" : \"111\" }", "mgr1")
  159. ) {
  160. xlog("first response from mongodb is [[$mongodb(mgr1=>value)]]\n");
  161. while(mongodb_next("mgr1") ){
  162. xlog("next response from mongodb is [[$mongodb(mgr1=>value)]]\n");
  163. }
  164. }
  165. mongodb_free("mgr1");
  166. ...
  167. 4.5. mongodb_free(replyid)
  168. Frees data in a previous reply from memory. After this function call,
  169. accessing to a freed replyid returns null value.
  170. It is not really necessary to free a reply to use it again in a new
  171. mongodb_cmd function, being automatically freed on next command
  172. execution, but for freeing used resources (e.g., memory), it is
  173. recommended to do it.
  174. Example 1.6. mongodb_free usage
  175. ...
  176. if(mongodb_cmd_simple("mgs1", "kamailio", "acc", "{ \"collStats\": \"acc\" }", "
  177. mgr1")) {
  178. xlog("response from mongodb is [[$mongodb(mgr1=>value)]]\n");
  179. }
  180. mongodb_free("mgr1");
  181. ...