Using MongoDB files
About this task
Procedure
- Compile and link the COBOL programs in your application by using the cob2 command.
-
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.
- 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
- 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.
- Start the MongoDB server using the following
command:
sudo systemctl start mongod
- 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
- 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:
- 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>.
- 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.
- To communicate with a MongoDB server, you need a connection URI. Set the VFS_MONGODB_URI
environment variable, use the following
command:
orexport VFS_MONGODB_URI=“mongodb://127.0.0.1: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.export VFS_MONGODB_URI=“mongodb://localhost:27017”
- To identify your MongoDB database, set the environment variable VFS_MONGODB_DATABASE using the
command:
- Set the COBRTOPT environment variable to indicate you are working with MongoDB
files:
export COBRTOPT=FILESYS=MONGO
- 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.
- To show the contents of a MongoDB file, or to remove a MongoDB file, use the vfs_cat and
vfs_rm utilities. For
example:
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.vfs_cat mongo-<database>.<filename> vfs_rm mongo-<database>.<filename>
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.