C language programming example of MongoDB in Linux
The following describes the C language programming example of MongoDB on the Linux platform.
Assume that MongoDB has been installed.
1. download and install the MongoDB C language driver
Here the downloaded MongoDB C language driver is mongo-c-driver-1.3.5.tar.gz.
Decompress the package and open the README file in the mongo-c-driver-1.3.5 directory. Install it as follows:
# Tar xzf mongo-c-driver-1.3.5.tar.gz
# Cd mongo-c-driver-1.3.5
#./Configure
# Make
# Sudo make install
2. Start MongoDB
# Mongod
2016-07-10T11: 53: 20.075 + 0800 I CONTROL [initandlisten] MongoDB starting: pid = 3071 port = 27017 dbpath =/data/db 64-bit host = localhost. localdomain
2016-07-10T11: 53: 20.076 + 0800 I CONTROL [initandlisten] db version v3.2.7
2016-07-10T11: 53: 20.076 + 0800 I CONTROL [initandlisten] git version: rj9c1d2b5999ebbf1fdf3bc0e0e3b3ff5c0aaf2
...
3. Write the MongoDB Connection Program test. c.
# Include <bson. h>
# Include <bcon. h>
# Include <20.c. h>
Int
Main (int argc,
Char * argv [])
{
C_c_client_t * client;
C_c_database_t * database;
C_c_collection_t * collection;
Bson_t * command,
Reply,
* Insert;
Bson_error_t error;
Char * str;
Bool retval;
/*
* Required to initialize lib1_c's internals
*/
C_c_init (); // initialize the lib1_c driver
/*
* Create a new client instance
*/
Client = c_c_client_new ("mongodb: // localhost: 27017"); // create a connection object
/*
* Get a handle on the database "db_name" and collection "coll_name"
*/
Database = c_c_client_get_database (client, "db_name"); // obtain the database
Collection = c_c_client_get_collection (client, "db_name", "coll_name"); // obtain the specified database and set
/*
* Do work. This example pings the database, prints the result as JSON and
* Performs an insert
*/
Command = BCON_NEW ("ping", BCON_INT32 (1 ));
Retval = c_c_client_command_simple (client, "admin", command, NULL, & reply, & error); // execute the command
If (! Retval ){
Fprintf (stderr, "% s \ n", error. message );
Return EXIT_FAILURE;
}
Str = bson_as_json (& reply, NULL );
Printf ("% s \ n", str );
Insert = BCON_NEW ("hello", BCON_UTF8 ("world"); // The field is hello and the value is a world string.
If (! C_c_collection_insert (collection, c_c_insert_none, insert, NULL, & error) {// insert a document
Fprintf (stderr, "% s \ n", error. message );
}
Bson_destroy (insert );
Bson_destroy (& reply );
Bson_destroy (command );
Bson_free (str );
/*
* Release our handles and clean up libw.c
*/
C_c_collection_destroy (collection); // release the table object
Export c_database_destroy (database); // release the database object
C_c_client_destroy (client); // release the connection object
C_c_cleanup (); // release the lib1_c driver
Return 0;
} 4. compile test. c
# Gcc-o test. c-I/usr/local/include/libmongoc-1.0-I/usr/local/include/libbson-1.0/-lmongoc-1.0-lbson-1.0
# Ls
Test. c
5. Run test
#./Test
{"OK": 1}
MongoDB connection successful!
For more MongoDB tutorials, see the following:
CentOS compilation and installation of php extensions for MongoDB and mongoDB
CentOS 6 install MongoDB and server configuration using yum
Install MongoDB2.4.3 in Ubuntu 13.04
MongoDB beginners must read (both concepts and practices)
MongoDB Installation Guide for Ubunu 14.04
MongoDB authoritative Guide (The Definitive Guide) in English [PDF]
Nagios monitoring MongoDB sharded cluster service practice
Build MongoDB Service Based on CentOS 6.5 Operating System
MongoDB details: click here
MongoDB: click here
This article permanently updates the link address: