Using MongoDB files

About this task

To access MongoDB files from a COBOL application that runs under Linux, you must follow this procedure for compiling and linking the application and for identifying the MongoDB database and files.

Procedure

  1. Compile and link the COBOL programs in your application by using the cob2 command.
  2. Check that the MongoDB server is installed and running. For details on installing and configuring MongoDB, see the list of related references. Note that you will also need to install the MongoDB C Driver available from mongoc.org as documented in the System prerequisites of the IBM COBOL for Linux Installation Guide.
    1. After installing MongoDB, there should be a directory for the mongod instance to store its data. For example, /var/lib/mongo. Verify the ownership is mongod, and if not, set it using this command:
      chown -R mongod:mongod /var/lib/mongo
    2. MongoDB creates a socket file for the database connection in the /tmp directory. Change the ownership of the socket file to mongod using the following command:
      chown mongod:mongod mongodb-<port>.sock

      where <port> is the default port of 27017, or another value chosen by the database admin.

    3. Start the MongoDB server using the following command:
      sudo systemctl start mongod
    4. Check the status of the MongoDB server using the following commands:
      sudo systemctl status mongod
      If the server is running, you should see output similar to the following:
      mongod   587399     1 13 10:03  ?   00:00:00 /usr/bin/mongod -f /etc/mongod.conf
      If the server doesn’t start using the systemctl command, manually start it using the command:
      /usr/bin/mongod -f /etc/mongod.conf
  3. MongoDB stores information as structured or unstructured objects called documents. These documents are grouped into collections. A MongoDB file has a two-part name:
    [<database>.]<filename>.

    In MongoDB terms, the database is <database> and the collection is <filename>.

    1. To identify your MongoDB database, set the environment variable VFS_MONGODB_DATABASE using the command:
      export VFS_MONGODB_DATABASE=<sampledb>

      where sampledb is the name of your MongoDB database.

    2. To communicate with a MongoDB server, you need a connection URI. Set the VFS_MONGODB_URI environment variable, use the following command:
      export VFS_MONGODB_URI=“mongodb://127.0.0.1:27017”
      or
      export VFS_MONGODB_URI=“mongodb://localhost:27017”
      where 27017 is the default port. If you chose a different port number in step 2b, update the VFS_MONGODB_URI to that same port number.
  4. Set the COBRTOPT environment variable to indicate you are working with MongoDB files:
    export COBRTOPT=FILESYS=MONGO
  5. Run your COBOL program. Any OPEN statements in your program will open the file in the MongoDB server.
    When you CLOSE the file, it will save it in the MongoDB server.
  6. To show the contents of a MongoDB file, or to remove a MongoDB file, use the vfs_cat and vfs_rm utilities. For example:
    vfs_cat mongo-<database>.<filename>
    vfs_rm mongo-<database>.<filename>
    
    Note that vfs_cat will display the file as if it was a sequential file, ordered write order (sequential), relative record number, or primary key.
    Alternately, if you have set the VFS_MONGODB_DATABASE environment variable to your MongoDB database name, you can use the following commands:
    vfs_cat mongo-<filename>
    vfs_rm mongo-<filename>
    

What to do next

For more information about Mongo, refer to the related tasks and references below.