Querying Mongodb From Kamailio 4.4.4 On Ubuntu Server

Explains the procedures in querying MongoDB collection and parse the JSON document returned, to use it later

We will discuss the procedures in querying MongoDB collection and parse the JSON document returned, to use it laterthroughout Kamailio routes.

We make useof following Kamailio modules:

ndb_mongodb module for querying MongoDB

jansson moduleto parse the JSON document data

We have already installed the mongo-c-driver thatis a dependency for this module. If you have not installed, refer to ourprevious blog.

Load ndb_mongodb module and configure in your kamailioconfiguration

# under modules section in yourkamailio.cfg

loadmodule"ndb_mongodb.so"  #

Configure the mongodb server path inmodule parameters modparam("ndb_mongodb", "server", "name=apayaa;uri=mongodb://admin:password@localhost/database")

There are many functions provided by the ndb_mongodb module to queryMongoDB.

  1. mongodb_cmd
  2. mongodb_cmd_simple
  3. mongodb_find
  4. mongodb_find_one
  5. mongodb_next
  6. mongodb_free

You can refer to Documentation above for details on functions providedby the ndb_mongodb module.

Example on using mongodb_find_one function:

mongodb_find_one: This will query the database and return firstmatching response.

if(mongodb_find_one("server","database", "employee", "{ "username" :"1001" }", "response")) {

xlog("L_INFO","Employee Data from mongodb is " $mongodb(response=>value)" n");

$var(jsondata) =$mongodb(response=>value);


You will have the response JSON Data available in this container $mongodb(response=>value) and we assign to $var(jsondata) variable.

As we haveJSON data available, now we will parse the response document to get the values,using JANSSON module.

JANSSONmodule configuration

Install the Jansson library dependency

cd /usr/src/

# Download using GIT git clonehttps://github.com/akheron/jansson.git

# Compile JANSSON

cd /usr/src/jansson autoreconf -i

./configure && make &&make install

Load Janssonmodule and configure in your Kamailio configuration.

# under modules section in yourkamailio.cfg

loadmodule “jansson.so”

There are no module parameters available.There are many functions provided by the Jansson module to parse JSON documents.

  1. jansson_get
  2. jansson_set
  3. jansson_append
  4. jansson_array_size
  5. jansson_get_field

You can refer to Documentation above for details on functions providedby the Jansson module.

Example on using jansson_get function:

jansson_get: Fetch the value at the provided ‘path’ fromthe json object and store it in result variable. This also supports dotdelimited notation to parse the json tree object.

eg. employee.contact.phone[0]

jansson_get("employee.contact.phone[0]",$var(jsondata), "$var(phonenumber)");

xlog("L_INFO","Employee Contact Phone number is $var(phonenumber)");

Now we have employee’s contact phone number in a variable, $var(phonenumber).We can use this anywhere in our script. Similarly we can fetch any data fromour JSON response document from mongodb.

The ndb_mongodb and Jansson modules are helpful when you need to fetch data in real time from mongodb and use it in your Kamailio routes.

The data fetching and parsing will be pretty fast, when using in-built modules rather than using external scripts.

License: You have permission to republish this article in any format, even commerically, but you must keep all links intact. Attribution required. Republishing formats.


Most Read

Using this website means you accept our Terms and Privacy Policy. Content published by users is licensed under their selected license.

Please be vigilant when exploring external websites linked from the articles/ads/profiles on this website.

© otherarticles™ 2017 | Site images and design © to Otherarticles (OA).