Linux平台下MongoDB的C語言編程執行個體

來源:互聯網
上載者:User

Linux平台下MongoDB的C語言編程執行個體

下面講述在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擴充

CentOS 6 使用 yum 安裝MongoDB及伺服器端配置

Ubuntu 13.04下安裝MongoDB2.4.3

MongoDB入門必讀(概念與實戰並重)

Ubunu 14.04下MongoDB的安裝指南

《MongoDB 權威指南》(MongoDB: The Definitive Guide)英文文字版[PDF]

Nagios監控MongoDB分區叢集服務實戰

基於CentOS 6.5作業系統搭建MongoDB服務

MongoDB 的詳細介紹:請點這裡
MongoDB 的:請點這裡

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.