阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Linux平台下MongoDB的C语言编程实例

96次阅读
没有评论

共计 3010 个字符,预计需要花费 8 分钟才能阅读完成。

下面讲述在 Linux 平台下 MongoDB 的 C 语言编程实例

假设已经安装好了 MongoDB。

1. 下载 MongoDB 的 C 语言驱动并安装

这里下载的 MongoDB 的 C 语言驱动是 mongo-c-driver-1.3.5.tar.gz。

解压后打开 mongo-c-driver-1.3.5 目录下的 README 文件,按其中讲的方法安装,如下:

  # tar xzf mongo-c-driver-1.3.5.tar.gz
  # cd mongo-c-driver-1.3.5
  # ./configure
  # make
  # sudo make install

2. 启动 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: 4249c1d2b5999ebbf1fdf3bc0e0e3b3ff5c0aaf2

3. 编写连接 MongoDB 的程序 test.c

#include <bson.h>
#include <bcon.h>
#include <mongoc.h>
int
main (int  argc,
      char *argv[])
{
  mongoc_client_t      *client;
  mongoc_database_t    *database;
  mongoc_collection_t  *collection;
  bson_t              *command,
                        reply,
                        *insert;
  bson_error_t          error;
  char                *str;
  bool                  retval;
  /*
    * Required to initialize libmongoc’s internals
    */
  mongoc_init ();// 初始化 libmongoc 驱动
  /*
    * Create a new client instance
    */
  client = mongoc_client_new (“mongodb://localhost:27017”);// 创建连接对象
  /*
    * Get a handle on the database “db_name” and collection “coll_name”
    */
  database = mongoc_client_get_database (client, “db_name”);// 获取数据库
  collection = mongoc_client_get_collection (client, “db_name”, “coll_name”);// 获取指定数据库和集合
  /*
    * Do work. This example pings the database, prints the result as JSON and
    * performs an insert
    */
  command = BCON_NEW (“ping”, BCON_INT32 (1));
  retval = mongoc_client_command_simple (client, “admin”, command, NULL, &reply, &error);// 执行命令
  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”));// 字段为 hello,值为 world 字符串
  if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, insert, NULL, &error)) {// 插入文档
      fprintf (stderr, “%s\n”, error.message);
  }
  bson_destroy (insert);
  bson_destroy (&reply);
  bson_destroy (command);
  bson_free (str);
  /*
    * Release our handles and clean up libmongoc
    */
  mongoc_collection_destroy (collection);// 释放表对象
  mongoc_database_destroy (database);// 释放数据库对象
  mongoc_client_destroy (client);// 释放连接对象
  mongoc_cleanup ();// 释放 libmongoc 驱动
  return 0;
}4. 编译 test.c

# gcc -o test test.c -I/usr/local/include/libmongoc-1.0 -I/usr/local/include/libbson-1.0/ -lmongoc-1.0 -lbson-1.0

# ls
test  test.c

5. 运行 test

# ./test
{“ok” : 1}

连接 MongoDB 成功!

更多 MongoDB 相关教程见以下内容

CentOS 编译安装 MongoDB 与 mongoDB 的 php 扩展 http://www.linuxidc.com/Linux/2012-02/53833.htm

CentOS 6 使用 yum 安装 MongoDB 及服务器端配置 http://www.linuxidc.com/Linux/2012-08/68196.htm

Ubuntu 13.04 下安装 MongoDB2.4.3 http://www.linuxidc.com/Linux/2013-05/84227.htm

MongoDB 入门必读(概念与实战并重) http://www.linuxidc.com/Linux/2013-07/87105.htm

Ubunu 14.04 下 MongoDB 的安装指南 http://www.linuxidc.com/Linux/2014-08/105364.htm

《MongoDB 权威指南》(MongoDB: The Definitive Guide)英文文字版[PDF] http://www.linuxidc.com/Linux/2012-07/66735.htm

Nagios 监控 MongoDB 分片集群服务实战 http://www.linuxidc.com/Linux/2014-10/107826.htm

基于 CentOS 6.5 操作系统搭建 MongoDB 服务 http://www.linuxidc.com/Linux/2014-11/108900.htm

MongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-07/133317.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计3010字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中