| 1 |
NDB_MONGODB Module |
| 2 |
|
| 3 |
Daniel-Constantin Mierla |
| 4 |
|
| 5 |
<[email protected]> |
| 6 |
|
| 7 |
Edited by |
| 8 |
|
| 9 |
Daniel-Constantin Mierla |
| 10 |
|
| 11 |
<[email protected]> |
| 12 |
|
| 13 |
Copyright © 2014 asipto.com |
| 14 |
__________________________________________________________________ |
| 15 |
|
| 16 |
Table of Contents |
| 17 |
|
| 18 |
1. Admin Guide |
| 19 |
|
| 20 |
1. Overview |
| 21 |
2. Dependencies |
| 22 |
|
| 23 |
2.1. Kamailio Modules |
| 24 |
2.2. External Libraries or Applications |
| 25 |
|
| 26 |
3. Parameters |
| 27 |
|
| 28 |
3.1. server (str) |
| 29 |
|
| 30 |
4. Functions |
| 31 |
|
| 32 |
4.1. mongodb_cmd(srvname, dbname, cname, command, replyid) |
| 33 |
4.2. mongodb_cmd_simple(srvname, dbname, cname, command, |
| 34 |
replyid) |
| 35 |
|
| 36 |
4.3. mongodb_find(srvname, dbname, cname, command, replyid) |
| 37 |
4.4. mongodb_find_one(srvname, dbname, cname, command, |
| 38 |
replyid) |
| 39 |
|
| 40 |
4.5. mongodb_next(replyid) |
| 41 |
4.6. mongodb_free(replyid) |
| 42 |
|
| 43 |
List of Examples |
| 44 |
|
| 45 |
1.1. Set server parameter |
| 46 |
1.2. mongodb_cmd usage |
| 47 |
1.3. mongodb_cmd_simple usage |
| 48 |
1.4. mongodb_find usage |
| 49 |
1.5. mongodb_find_one usage |
| 50 |
1.6. mongodb_next usage |
| 51 |
1.7. mongodb_free usage |
| 52 |
|
| 53 |
Chapter 1. Admin Guide |
| 54 |
|
| 55 |
Table of Contents |
| 56 |
|
| 57 |
1. Overview |
| 58 |
2. Dependencies |
| 59 |
|
| 60 |
2.1. Kamailio Modules |
| 61 |
2.2. External Libraries or Applications |
| 62 |
|
| 63 |
3. Parameters |
| 64 |
|
| 65 |
3.1. server (str) |
| 66 |
|
| 67 |
4. Functions |
| 68 |
|
| 69 |
4.1. mongodb_cmd(srvname, dbname, cname, command, replyid) |
| 70 |
4.2. mongodb_cmd_simple(srvname, dbname, cname, command, replyid) |
| 71 |
4.3. mongodb_find(srvname, dbname, cname, command, replyid) |
| 72 |
4.4. mongodb_find_one(srvname, dbname, cname, command, replyid) |
| 73 |
4.5. mongodb_next(replyid) |
| 74 |
4.6. mongodb_free(replyid) |
| 75 |
|
| 76 |
1. Overview |
| 77 |
|
| 78 |
This module provides a non-db connector to interact with MongoDB NoSQL |
| 79 |
server from configuration file. You can read more about MongoDB at |
| 80 |
http://www.mongodb.org. |
| 81 |
|
| 82 |
It can connect to many MongoDB servers and store the results in |
| 83 |
different containers. |
| 84 |
|
| 85 |
2. Dependencies |
| 86 |
|
| 87 |
2.1. Kamailio Modules |
| 88 |
2.2. External Libraries or Applications |
| 89 |
|
| 90 |
2.1. Kamailio Modules |
| 91 |
|
| 92 |
The following modules must be loaded before this module: |
| 93 |
* none. |
| 94 |
|
| 95 |
2.2. External Libraries or Applications |
| 96 |
|
| 97 |
The following libraries or applications must be installed before |
| 98 |
running Kamailio with this module loaded: |
| 99 |
* mongo-c-driver - available at |
| 100 |
https://github.com/mongodb/mongo-c-driver |
| 101 |
Note: if you use tls module, use at least mongo-c-driver v1.3.5 and |
| 102 |
compile the library by configuring it without automatic init and |
| 103 |
cleanup (you have to run: './configure |
| 104 |
--disable-automatic-init-and-cleanup') -- this option is planned to |
| 105 |
be removed in future versions. An alternative for v1.3.5 could be |
| 106 |
the patch from: |
| 107 |
https://github.com/miconda/mongo-c-driver/commit/51d95009de39eaeca4 |
| 108 |
8491682a5ffec4f83cde55 |
| 109 |
|
| 110 |
3. Parameters |
| 111 |
|
| 112 |
3.1. server (str) |
| 113 |
|
| 114 |
3.1. server (str) |
| 115 |
|
| 116 |
Specify the details to connect to MongoDB server. It takes a list of |
| 117 |
attribute=value separated by semicolon, the attributes can be name and |
| 118 |
uri. Name is a generic identifier to be used with module functions. The |
| 119 |
uri parameter must be a valid MongoDB database connection string. |
| 120 |
|
| 121 |
You can set this parameter many times, in case you want to connect to |
| 122 |
many MongoDB servers, just give different attributes and use the |
| 123 |
specific server name when querying the MongoDB instance. |
| 124 |
|
| 125 |
Default value is NULL. |
| 126 |
|
| 127 |
Example 1.1. Set server parameter |
| 128 |
... |
| 129 |
modparam("ndb_mongodb", "server", "name=mgs1;uri='mongodb://localhost/kamailio'" |
| 130 |
) |
| 131 |
modparam("ndb_mongodb", "server", "name=mgs2;uri='mongodb://127.0.0.2/kamailio'" |
| 132 |
) |
| 133 |
... |
| 134 |
|
| 135 |
4. Functions |
| 136 |
|
| 137 |
4.1. mongodb_cmd(srvname, dbname, cname, command, replyid) |
| 138 |
4.2. mongodb_cmd_simple(srvname, dbname, cname, command, replyid) |
| 139 |
4.3. mongodb_find(srvname, dbname, cname, command, replyid) |
| 140 |
4.4. mongodb_find_one(srvname, dbname, cname, command, replyid) |
| 141 |
4.5. mongodb_next(replyid) |
| 142 |
4.6. mongodb_free(replyid) |
| 143 |
|
| 144 |
4.1. mongodb_cmd(srvname, dbname, cname, command, replyid) |
| 145 |
|
| 146 |
Send a valid command to MongoDB server identified by srvname. The reply |
| 147 |
will be stored in a local container identified by replyid. All the |
| 148 |
parameters can be strings with pseudo-variables that are evaluated at |
| 149 |
runtime. |
| 150 |
|
| 151 |
The reply can be accessed via pseudo-variable $mongodb(key). The key |
| 152 |
can be: type - type of the reply; value - the value in JSON format |
| 153 |
returned by MongoDB server; info - in case of error from MongoDB, it |
| 154 |
will contain an info message. |
| 155 |
|
| 156 |
The result can contain many documents, see mongodb_next() function for |
| 157 |
more details. |
| 158 |
|
| 159 |
Meaning of the parameters: |
| 160 |
* srvname - MongoDB server name as given via 'server' parameter. |
| 161 |
* dbname - MongoDB database name. |
| 162 |
* cname - MongoDB collection (table) name. |
| 163 |
* command - valid MongoDB command given as JSON string. |
| 164 |
* replyid - the name of the container to store the response. |
| 165 |
|
| 166 |
The function can be used from ANY_ROUTE. |
| 167 |
|
| 168 |
Example 1.2. mongodb_cmd usage |
| 169 |
... |
| 170 |
if(mongodb_cmd("mgs1", "kamailio", "acc", "{ \"collStats\": \"acc\" }", "mgr1")) |
| 171 |
{ |
| 172 |
xlog("response from mongodb is [[$mongodb(mgr1=>value)]]\n"); |
| 173 |
} |
| 174 |
... |
| 175 |
|
| 176 |
4.2. mongodb_cmd_simple(srvname, dbname, cname, command, replyid) |
| 177 |
|
| 178 |
Send a valid command to MongoDB server identified by srvname. The reply |
| 179 |
will be stored in a local container identified by replyid. All the |
| 180 |
parameters can be strings with pseudo-variables that are evaluated at |
| 181 |
runtime. |
| 182 |
|
| 183 |
The reply can be accessed via pseudo-variable $mongodb(key). The key |
| 184 |
can be: type - type of the reply; value - the value in JSON format |
| 185 |
returned by MongoDB server; info - in case of error from MongoDB, it |
| 186 |
will contain an info message. |
| 187 |
|
| 188 |
This function should be used when the response has only one document. |
| 189 |
|
| 190 |
See mongodb_cmd() for the meaning of the parameters. |
| 191 |
|
| 192 |
The function can be used from ANY_ROUTE. |
| 193 |
|
| 194 |
Example 1.3. mongodb_cmd_simple usage |
| 195 |
... |
| 196 |
if(mongodb_cmd_simple("mgs1", "kamailio", "acc", "{ \"collStats\": \"acc\" }", " |
| 197 |
mgr1")) { |
| 198 |
xlog("response from mongodb is [[$mongodb(mgr1=>value)]]\n"); |
| 199 |
} |
| 200 |
... |
| 201 |
|
| 202 |
4.3. mongodb_find(srvname, dbname, cname, command, replyid) |
| 203 |
|
| 204 |
Send a find command to MongoDB server identified by srvname. The reply |
| 205 |
will be stored in a local container identified by replyid. All the |
| 206 |
parameters can be strings with pseudo-variables that are evaluated at |
| 207 |
runtime. |
| 208 |
|
| 209 |
The reply can be accessed via pseudo-variable $mongodb(key). The key |
| 210 |
can be: type - type of the reply; value - the value in JSON format |
| 211 |
returned by MongoDB server; info - in case of error from MongoDB, it |
| 212 |
will contain an info message. |
| 213 |
|
| 214 |
This function is useful for querying collections and accessing the |
| 215 |
results. |
| 216 |
|
| 217 |
See mongodb_cmd() for the meaning of the parameters, with the remark |
| 218 |
that command has to be a valid filter JSON document, like for find() |
| 219 |
command in mongoc command line client tool. |
| 220 |
|
| 221 |
The function can be used from ANY_ROUTE. |
| 222 |
|
| 223 |
Example 1.4. mongodb_find usage |
| 224 |
... |
| 225 |
if(mongodb_find("mgs1", "kamailio", "acc", "{ \"src_user\" : \"111\" }", "mgr1") |
| 226 |
) { |
| 227 |
xlog("response from mongodb is [[$mongodb(mgr1=>value)]]\n"); |
| 228 |
} |
| 229 |
... |
| 230 |
|
| 231 |
4.4. mongodb_find_one(srvname, dbname, cname, command, replyid) |
| 232 |
|
| 233 |
Similar to mongodb_find(...), but it stops searching after first match, |
| 234 |
returning the result with one object - faster when expecting to have a |
| 235 |
single match. Parameters and behaviour are the same as for |
| 236 |
mongodb_find(...). |
| 237 |
|
| 238 |
The function can be used from ANY_ROUTE. |
| 239 |
|
| 240 |
Example 1.5. mongodb_find_one usage |
| 241 |
... |
| 242 |
if(mongodb_find_one("mgs1", "kamailio", "subscriber", "{ \"username\" : \"111\" |
| 243 |
}", "mgr1")) { |
| 244 |
xlog("response from mongodb is [[$mongodb(mgr1=>value)]]\n"); |
| 245 |
} |
| 246 |
... |
| 247 |
|
| 248 |
4.5. mongodb_next(replyid) |
| 249 |
|
| 250 |
Moves to next document in a MongoDB reply. This function can be used |
| 251 |
after a successful mongodb_cmd() or mongodb_find(). It returns true if |
| 252 |
there is a shift to document. The current document becomes available |
| 253 |
via $mongodb(key) variable. |
| 254 |
|
| 255 |
Example 1.6. mongodb_next usage |
| 256 |
... |
| 257 |
if(mongodb_find("mgs1", "kamailio", "acc", "{ \"src_user\" : \"111\" }", "mgr1") |
| 258 |
) { |
| 259 |
xlog("first response from mongodb is [[$mongodb(mgr1=>value)]]\n"); |
| 260 |
while(mongodb_next("mgr1") ){ |
| 261 |
xlog("next response from mongodb is [[$mongodb(mgr1=>value)]]\n"); |
| 262 |
} |
| 263 |
} |
| 264 |
mongodb_free("mgr1"); |
| 265 |
... |
| 266 |
|
| 267 |
4.6. mongodb_free(replyid) |
| 268 |
|
| 269 |
Frees data in a previous reply from memory. After this function call, |
| 270 |
accessing to a freed replyid returns null value. |
| 271 |
|
| 272 |
It is not really necessary to free a reply to use it again in a new |
| 273 |
mongodb_cmd function, being automatically freed on next command |
| 274 |
execution, but for freeing used resources (e.g., memory), it is |
| 275 |
recommended to do it. |
| 276 |
|
| 277 |
Example 1.7. mongodb_free usage |
| 278 |
... |
| 279 |
if(mongodb_cmd_simple("mgs1", "kamailio", "acc", "{ \"collStats\": \"acc\" }", " |
| 280 |
mgr1")) { |
| 281 |
xlog("response from mongodb is [[$mongodb(mgr1=>value)]]\n"); |
| 282 |
} |
| 283 |
mongodb_free("mgr1"); |
| 284 |
... |