MongoDB C Driver使用教程


MongoDB C Driver使用教程

轉載請注明出處http://www.cnblogs.com/oloroso/

本指南提供簡介 MongoDB C 驅動程序。
在 C API 的詳細信息,請參閱API 文檔.
原文來自http://api.mongodb.com/c/current/tutorial.html

0.安裝

有關特定的平台上安裝MongoDB C驅動程序的詳細說明,請參閱安裝指南.

MongoDB C 驅動程序的安裝
編譯 http://www.cnblogs.com/oloroso/p/5740431.html

1.啟動MongoDB

要運行本教程中的例子,MongoDB 必須安裝運行在本地主機(localhost),且使用默認端口27017。要檢查MongoDB是否啟動並運行,使用MongoDB Shell連接就知道了。

$ mongo --host localhost --port 27017
MongoDB shell version: 3.0.6
connecting to: localhost:27017/test
> 

2.進行連接

MongoDB C Driver程序通過mongoc_client_t提供了一種簡便的訪問MongoDB的方法( 與集群配置無關的)。
它滿足透明地連接到獨立的服務器,副本集和分片集群上的需求。一旦建立了連接,數據庫和集合的句柄可以通過結構mongoc_database_tmongoc_collection_t分別得到。然后可以通過這些句柄執行MongoDB操作。

在應用程序的啟動后,先調用mongoc_init()libmongoc 的任何其他功能才能正確使用,並需要在退出之前調用mongoc_cleanup()。當創建client、database和server的句柄后,需要在使用完后調用適當的銷毀函數。

下面的示例建立一個獨立的服務器上的本地主機的連接,並執行一個簡單的命令。有關數據庫操作的詳細信息可以查看CRUD 操作執行命令部分。連接到副本集和分片集群的例子可以在高級連接頁面查看。

connect.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;

   /*
    * 初始化libmongoc's
    */
   mongoc_init ();

   /*
    * 創建一個新的client實例
    */
   client = mongoc_client_new ("mongodb://localhost:27017");

   /*
    * 獲取數據庫"db_name"和集合"coll_name"的句柄
    */
   database = mongoc_client_get_database (client, "db_name");
   collection = mongoc_client_get_collection (client, "db_name", "coll_name");

   /*
    * 執行操作。此處以執行ping數據庫,以json格式打印結果。並執行一個插入操作。
    */

   // 執行命令操作(ping)
   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;
   }
   // 獲取json形式的結果
   str = bson_as_json (&reply, NULL);
   printf ("%s\n", str);	// 打印輸出

   // 插入操作命令
   insert = BCON_NEW ("hello", BCON_UTF8 ("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);

   /*
    * 釋放擁有的句柄並清理libmongoc
    */
   mongoc_collection_destroy (collection);
   mongoc_database_destroy (database);
   mongoc_client_destroy (client);
   mongoc_cleanup ();

   return 0;
}

在 unix 系統中,代碼可以編譯和運行像這樣︰

$ gcc -o connect connect.c $(pkg-config --cflags --libs libmongoc-1.0)
$ ./connect
{ "ok" : 1.000000 }

或者,如果pkg 配置不可用,可以手動管理路徑和庫文件。

$ gcc -o connect connect.c -I/usr/local/include -lmongoc-1.0 -lbson-1.0
$ ./connect
{ "ok" : 1.000000 }

對於 Windows 用戶,可以編譯和運行下面的命令代碼。(這假定MongoDB C Driver程序安裝在C:\mongo-c-driver; 安裝在其他位置的根據需要更改包含目錄。)

C:\> cl.exe /IC:\mongo-c-driver\include\libbson-1.0 /IC:\mongo-c-driver\include\libmongoc-1.0 connect.c
C:\> connect
{ "ok" : 1.000000 }

查看更多

3.創建 BSON 文件

BSON文檔(Documents)是存儲在MongoDB的數據格式。C Driver程序使用libbson來創建BSON文檔。
有幾種方式來構建它們:1、追加key-value(鍵值對);2、使用BCON;3、解析JSON字符串。

1、追加BSON

BSON文檔,在代碼中使用bson_t表示。可以將構造一個字段,使用libbsonappend功能進行追加。

#include <bson.h>

int
main (int   argc,
      char *argv[])
{
   bson_t *document;
   bson_t  child;
   char   *str;

   document = bson_new ();

   /*
	* 追加{"hello" : "world"}到document.
    * 傳遞 -1 作為長度參數,告訴libbson自己去計算字符串長度.
    */
   bson_append_utf8 (document, "hello", -1, "world", -1);

   /*
    * 方便使用的, 這個宏與上面是等效的。
    */
   BSON_APPEND_UTF8 (document, "hello", "world");

   /*
    * 開始一個子文檔
    */
   BSON_APPEND_DOCUMENT_BEGIN (document, "subdoc", &child);
   BSON_APPEND_UTF8 (&child, "subkey", "value");
   bson_append_document_end (document, &child);

   /*
    * 打印這個BSON對象,以json字符串格式。
    */
   str = bson_as_json (document, NULL);
   printf ("%s\n", str);
   bson_free (str);

   /*
    * 清理已分配的bson documents.
    */
   bson_destroy (document);
   return 0;
}

請參閱libbson文檔的所有可附加到bson_t的類型.

2、使用BCON

BCONBSONC對象表示法的簡稱,是另一種以更接近預定的格式構建BSON文檔的方式。它有在類型安全上比BSONappend功能弱,但使用的代碼更少。

#include <bcon.h>
#include <bson.h>
#include <stdio.h>

int
main (int   argc,
      char *argv[])
{
   bson_t *doc;
   char *str;

   doc = BCON_NEW ("name", BCON_UTF8 ("Babe Ruth"),
                   "statistics", "{",
                      "batting_average", BCON_DOUBLE (.342),
                      "hits", BCON_INT32 (2873),
                      "home_runs", BCON_INT32 (714),
                      "rbi", BCON_INT32 (2213),
                   "}",
                   "nicknames", "[",
                      BCON_UTF8 ("the Sultan of Swat"),
                      BCON_UTF8 ("the Bambino"),
                   "]");

   str = bson_as_json (doc, NULL);
   printf ("%s\n", str);
   bson_free (str);

   bson_destroy (doc);

   return 0;
}

請注意,BCON可以創建數組、子文檔以及任意字段。

3、從JSON創建BSON

JSON字符串可以通過bson_new_from_json創建單個BSON文檔。
若要從一個JSON文檔序列初始化BSON,使用bson_json_reader_t

#include <bson.h>

int
main (int   argc,
      char *argv[])
{
   bson_error_t error;
   bson_t      *bson;
   char        *string;

   const char *json = "{\"hello\": \"world\"}";
   bson = bson_new_from_json ((const uint8_t *)json, -1, &error);

   if (!bson) {
      fprintf (stderr, "%s\n", error.message);
      return EXIT_FAILURE;
   }

   string = bson_as_json (bson, NULL);
   printf ("%s\n", string);
   bson_free (string);

   return 0;
}

更多的信息

4.基本的CRUD(增刪查改)操作

本節演示如何使用C Driver程序與MongoDB進行交互的基本知識。

1、插入文檔

若要向集合中插入文件,首先通過mongoc_client_t獲取mongoc_collection_t的句柄。然后使用mongoc_collection_insert()BSON文檔添加到集合。
本示例將插入記錄到數據庫"mydb"和集合"mycoll"。

當完成后,確保分配的結構使用他們各自的銷毀函數進行釋放。

insert.c

#include <bson.h>
#include <mongoc.h>
#include <stdio.h>

int
main (int   argc,
      char *argv[])
{
    mongoc_client_t *client;
    mongoc_collection_t *collection;
    bson_error_t error;
    bson_oid_t oid;
    bson_t *doc;

	// 初始化libmongoc
    mongoc_init ();
	
	// 連接到數據庫,並獲取集合句柄
    client = mongoc_client_new ("mongodb://localhost:27017/");
    collection = mongoc_client_get_collection (client, "mydb", "mycoll");

	// 創建一個bson文檔,並追加鍵值對數據
    doc = bson_new ();
    bson_oid_init (&oid, NULL);
    BSON_APPEND_OID (doc, "_id", &oid);
    BSON_APPEND_UTF8 (doc, "hello", "world");

	// 將bson文檔插入到集合
    if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
        fprintf (stderr, "%s\n", error.message);
    }
	// 釋放資源
    bson_destroy (doc);
    mongoc_collection_destroy (collection);
    mongoc_client_destroy (client);

	// 清理libmongoc
    mongoc_cleanup ();

    return 0;
}

對代碼進行編譯並運行︰
Linux和Unix下:

$ gcc -o insert insert.c $(pkg-config --cflags --libs libmongoc-1.0)
$ ./insert

Windows下:

C:\> cl.exe /IC:\mongo-c-driver\include\libbson-1.0 /IC:\mongo-c-driver\include\libmongoc-1.0 insert.c
C:\> insert

若要驗證插入成功與否,使用MongoDB Shell進行查看。

$ mongo
MongoDB shell version: 3.0.6
connecting to: test
> use mydb
switched to db mydb
> db.mycoll.find()
{ "_id" : ObjectId("55ef43766cb5f36a3bae6ee4"), "hello" : "world" }
> 

更多的信息

2、查找文檔

若使用C Dirver程序去查詢MongoDB 集合,請使用函數mongoc_collection_find()](http://api.mongodb.com/c/current/mongoc_collection_find.html)。將返回一個匹配的文檔的cursor(游標)。

下面示例循環訪問result cursor(結果游標)並打印到為JSON字符串到標准輸出。

請注意,mongoc_collection_find使用BSON文檔作為查詢說明符;
例如,{ "color" : "red" }將匹配所有文檔中字段名為color且值為red的。空文檔{}可以用於匹配的所有文件。

這第一個示例使用空查詢說明符,來找到所有"mydb"數據庫中"mycoll"集合的文檔。

find.c

#include <bson.h>
#include <mongoc.h>
#include <stdio.h>

int
main (int   argc,
     char *argv[])
{
  mongoc_client_t *client;
  mongoc_collection_t *collection;
  mongoc_cursor_t *cursor;
  const bson_t *doc;
  bson_t *query;
  char *str;

  mongoc_init ();

  client = mongoc_client_new ("mongodb://localhost:27017/");
  collection = mongoc_client_get_collection (client, "mydb", "mycoll");

  // query是一個空的BSON文檔,用於做查詢說明符的時候匹配所有文檔。
  query = bson_new ();
  // 執行查詢操作
  cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);

  while (mongoc_cursor_next (cursor, &doc)) {
     str = bson_as_json (doc, NULL);
     printf ("%s\n", str);
     bson_free (str);
  }

  bson_destroy (query);
  mongoc_cursor_destroy (cursor);
  mongoc_collection_destroy (collection);
  mongoc_client_destroy (client);
  mongoc_cleanup ();

  return 0;
}

對代碼進行編譯和運行它︰
Linux和Unix下:

$ gcc -o find find.c $(pkg-config --cflags --libs libmongoc-1.0)
$ ./find
{ "_id" : { "$oid" : "55ef43766cb5f36a3bae6ee4" }, "hello" : "world" }

Windows下:

C:\> cl.exe /IC:\mongo-c-driver\include\libbson-1.0 /IC:\mongo-c-driver\include\libmongoc-1.0 find.c
C:\> find
{ "_id" : { "$oid" : "55ef43766cb5f36a3bae6ee4" }, "hello" : "world" }

若要查找特定的文檔,將添加指定的查詢說明符。本示例將添加對BSON_APPEND_UTF8()的調用來尋找匹配{"hello":"world"}的所有文檔。

find-specific.c

#include <bson.h>
#include <mongoc.h>
#include <stdio.h>

int
main (int   argc,
      char *argv[])
{
    mongoc_client_t *client;
    mongoc_collection_t *collection;
    mongoc_cursor_t *cursor;
    const bson_t *doc;
    bson_t *query;
    char *str;

    mongoc_init ();

    client = mongoc_client_new ("mongodb://localhost:27017/");
    collection = mongoc_client_get_collection (client, "mydb", "mycoll");

	// query在這里將添加一個鍵值對,查詢的時候將只查詢出匹配上的結果
    query = bson_new ();
    BSON_APPEND_UTF8 (query, "hello", "world");

    cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);

    while (mongoc_cursor_next (cursor, &doc)) {
        str = bson_as_json (doc, NULL);
        printf ("%s\n", str);
        bson_free (str);
    }

    bson_destroy (query);
    mongoc_cursor_destroy (cursor);
    mongoc_collection_destroy (collection);
    mongoc_client_destroy (client);
    mongoc_cleanup ();

    return 0;
}

編譯運行

$ gcc -o find-specific find-specific.c $(pkg-config --cflags --libs libmongoc-1.0)
$ ./find-specific
{ "_id" : { "$oid" : "55ef43766cb5f36a3bae6ee4" }, "hello" : "world" }

C:\> cl.exe /IC:\mongo-c-driver\include\libbson-1.0 /IC:\mongo-c-driver\include\libmongoc-1.0 find-specific.c
C:\> find-specific
{ "_id" : { "$oid" : "55ef43766cb5f36a3bae6ee4" }, "hello" : "world" }

更多的信息

3、更新文檔

這段代碼是使用mongoc_collection_update()來更新的文檔字段的示例。

下面的示例使用"mydb"數據庫,將文檔插入到"mycoll"集合。然后利用其_id字段,更新文檔的值(key對應的value),並添加一個新的字段(updated)。

update.c

#include <bcon.h>
#include <bson.h>
#include <mongoc.h>
#include <stdio.h>

int
main (int   argc,
      char *argv[])
{
    mongoc_collection_t *collection;
    mongoc_client_t *client;
    bson_error_t error;
    bson_oid_t oid;
    bson_t *doc = NULL;
    bson_t *update = NULL;
    bson_t *query = NULL;

    mongoc_init ();

    client = mongoc_client_new ("mongodb://localhost:27017/");
    collection = mongoc_client_get_collection (client, "mydb", "mycoll");

	// 初始化一個對象ID
    bson_oid_init (&oid, NULL);
    doc = BCON_NEW ("_id", BCON_OID (&oid)/*使用上面初始化的oid*/,
                    "key", BCON_UTF8 ("old_value"));

	// 插入到數據庫中
    if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
        fprintf (stderr, "%s\n", error.message);
        goto fail;
    }

	// 注意,這里使用的oid是之前插入一樣的
    query = BCON_NEW ("_id", BCON_OID (&oid));
    update = BCON_NEW ("$set", "{",
                           "key", BCON_UTF8 ("new_value")/*修改值*/,
                           "updated", BCON_BOOL (true)	/*添加的字段*/,
                       "}");
	// 執行update操作。這個操作將使用update的內容去替換之前插入到數據庫中的doc的內容
    if (!mongoc_collection_update (collection, MONGOC_UPDATE_NONE, query, update, NULL, &error)) {
        fprintf (stderr, "%s\n", error.message);
        goto fail;
    }

fail:
    if (doc)
        bson_destroy (doc);
    if (query)
        bson_destroy (query);
    if (update)
        bson_destroy (update);

    mongoc_collection_destroy (collection);
    mongoc_client_destroy (client);
    mongoc_cleanup ();

    return 0;
}

對代碼進行編譯和運行它︰
Linux和Unix下:

$ gcc -o update update.c $(pkg-config --cflags --libs libmongoc-1.0)
$ ./update

Windows下:

C:\> cl.exe /IC:\mongo-c-driver\include\libbson-1.0 /IC:\mongo-c-driver\include\libmongoc-1.0 update.c
C:\> update
{ "_id" : { "$oid" : "55ef43766cb5f36a3bae6ee4" }, "hello" : "world" }

若要驗證插入成功與否,使用MongoDB Shell進行查看。

$ mongo
MongoDB shell version: 3.0.6
connecting to: test
> use mydb
switched to db mydb
> db.mycoll.find({"updated" : true})
{ "_id" : ObjectId("55ef549236fe322f9490e17b"), "updated" : true, "key" : "new_value" }
> 

更多的信息

4、刪除文檔

本示例說明了使用mongoc_collection_remove()來刪除文檔。

下面的代碼插入一個示例文檔到數據庫"mydb"的"mycoll"集合。然后,它會刪除所有與{"hello":"world"}相匹配的文檔。

delete.c

#include <bson.h>
#include <mongoc.h>
#include <stdio.h>

int
main (int   argc,
      char *argv[])
{
    mongoc_client_t *client;
    mongoc_collection_t *collection;
    bson_error_t error;
    bson_oid_t oid;
    bson_t *doc;

    mongoc_init ();

    client = mongoc_client_new ("mongodb://localhost:27017/");
    collection = mongoc_client_get_collection (client, "test", "test");

    doc = bson_new ();
    bson_oid_init (&oid, NULL);
    BSON_APPEND_OID (doc, "_id", &oid);
    BSON_APPEND_UTF8 (doc, "hello", "world");	// 添加hello字段,值為world

	// 插入文檔
    if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
        fprintf (stderr, "Insert failed: %s\n", error.message);
    }

    bson_destroy (doc);

    doc = bson_new ();
    BSON_APPEND_OID (doc, "_id", &oid);	// 這里只添加了_id字段

	// 這里與上面描述的有點不一致,因為這里匹配的只是上面插入的,而不是含hello字段,且值為world的
	// 如果按照描述的話,這里應該改為BSON_APPEND_UTF8(doc,"hello","world")才行。

	// 執行刪除操作。這里只能匹配_id字段,也就只能刪除上面插入的文檔
    if (!mongoc_collection_remove (collection, MONGOC_REMOVE_SINGLE_REMOVE, doc, NULL, &error)) {
        fprintf (stderr, "Delete failed: %s\n", error.message);
    }

    bson_destroy (doc);
    mongoc_collection_destroy (collection);
    mongoc_client_destroy (client);
    mongoc_cleanup ();

    return 0;
}

對代碼進行編譯和運行它︰
Linux和Unix下:

$ gcc -o delete delete.c $(pkg-config --cflags --libs libmongoc-1.0)
$ ./delete

Windows下:

C:\> cl.exe /IC:\mongo-c-driver\include\libbson-1.0 /IC:\mongo-c-driver\include\libmongoc-1.0 delete.c
C:\> delete

使用MongoDB Shell證明文件已成功刪除。

$ mongo
MongoDB shell version: 3.0.6
connecting to: test
> use mydb
switched to db mydb
> db.mycoll.count({"hello" : "world"})
0
> 

更多的信息

5、統計文檔

這里的標題應該改為文檔計數會更合理。

計數MongoDB 集合中的文檔數目類似於執行查找操作。此示例在數據庫"mydb"中"mycoll"集合中獲取與{"hello":"world"}相匹配的文檔的數目。

count.c

#include <bson.h>
#include <mongoc.h>
#include <stdio.h>

int
main (int   argc,
      char *argv[])
{
   mongoc_client_t *client;
   mongoc_collection_t *collection;
   bson_error_t error;
   bson_t *doc;
   int64_t count;

   mongoc_init ();

   client = mongoc_client_new ("mongodb://localhost:27017/");
   collection = mongoc_client_get_collection (client, "mydb", "mycoll");

   // doc用於計數時候做匹配
   doc = bson_new_from_json ((const uint8_t *)"{\"hello\" : \"world\"}", -1, &error);
   
   // 進行統計操作
   count = mongoc_collection_count (collection, MONGOC_QUERY_NONE, doc, 0, 0, NULL, &error);

   if (count < 0) {
      fprintf (stderr, "%s\n", error.message);
   } else {
      printf ("%" PRId64 "\n", count);
   }

   bson_destroy (doc);
   mongoc_collection_destroy (collection);
   mongoc_client_destroy (client);
   mongoc_cleanup ();

   return 0;
}

對代碼進行編譯和運行它︰

Linux和Unix下:

$ gcc -o count count.c $(pkg-config --cflags --libs libmongoc-1.0)
$ ./count
1

Windows下:

C:\> cl.exe /IC:\mongo-c-driver\include\libbson-1.0 /IC:\mongo-c-driver\include\libmongoc-1.0 count.c
C:\> count
1

更多的信息

5.執行命令

驅動程序提供在客戶端執行MongoDB命令操作數據庫和集合結構的helper functions(輔助函數)。這些函數返回的cursor游標。這些函數的*_simple變體形式返回指示成功或失敗的布爾值。

本示例針對數據庫"mydb"中的"mycoll"集合執行collStats(獲取集合狀態信息)命令。

executing.c

#include <bson.h>
#include <bcon.h>
#include <mongoc.h>
#include <stdio.h>

int
main (int   argc,
      char *argv[])
{
    mongoc_client_t *client;
    mongoc_collection_t *collection;
    bson_error_t error;
    bson_t *command;
    bson_t reply;
    char *str;

    mongoc_init ();

    client = mongoc_client_new ("mongodb://localhost:27017/");
    collection = mongoc_client_get_collection (client, "mydb", "mycoll");

	// 創建命令(命令也是一個BSON文檔)
    command = BCON_NEW ("collStats",BCON_UTF8 ("mycoll"));
	// 執行命令。注意,這里使用的是_simple變體形式
    if (mongoc_collection_command_simple (collection, command, NULL, &reply, &error)) {
        str = bson_as_json (&reply, NULL);
        printf ("%s\n", str);
        bson_free (str);
    } else {
        fprintf (stderr, "Failed to run command: %s\n", error.message);
    }

    bson_destroy (command);
    bson_destroy (&reply);
    mongoc_collection_destroy (collection);
    mongoc_client_destroy (client);
    mongoc_cleanup ();

    return 0;
}

對代碼進行編譯和運行它︰
Linux和Unix下:

$ gcc -o executing executing.c $(pkg-config --cflags --libs libmongoc-1.0)
$ ./executing
{ "ns" : "mydb.mycoll", "count" : 1, "size" : 48, "avgObjSize" : 48, "numExtents" : 1, "storageSize" : 8192,
"lastExtentSize" : 8192.000000, "paddingFactor" : 1.000000, "userFlags" : 1, "capped" : false, "nindexes" : 1,
"indexDetails" : {  }, "totalIndexSize" : 8176, "indexSizes" : { "_id_" : 8176 }, "ok" : 1.000000 }

Windows下:

C:\> cl.exe /IC:\mongo-c-driver\include\libbson-1.0 /IC:\mongo-c-driver\include\libmongoc-1.0 executing.c
C:\> executing
{ "ns" : "mydb.mycoll", "count" : 1, "size" : 48, "avgObjSize" : 48, "numExtents" : 1, "storageSize" : 8192,
"lastExtentSize" : 8192.000000, "paddingFactor" : 1.000000, "userFlags" : 1, "capped" : false, "nindexes" : 1,
"indexDetails" : {  }, "totalIndexSize" : 8176, "indexSizes" : { "_id_" : 8176 }, "ok" : 1.000000 }

更多的信息

6.線程

MongoDB C Driver程序在絕大多數相關操作是線程未知的。這意味着它是由程序員來保證線程安全性。

然而,mongoc_client_pool_t(mongoc客戶端池)是線程安全,用於以線程安全的方式獲取mongoc_client_t。然后后從pool中檢索client,client structure應調用線程持有。該線程完成后,client應該放回池中。

#include <mongoc.h>
#include <pthread.h>

#define N_THREADS 10

// 線程工作函數
static void *
worker (void *data) {
   
	mongoc_client_pool_t *pool = data;
	mongoc_client_t      *client;

	// 從客戶端池中獲取一個客戶端
	client = mongoc_client_pool_pop (pool);

    /* Do something... */

	// 用完后需要還回客戶端池
	mongoc_client_pool_push (pool, client);

	return NULL;
}

int
main (int   argc,
      char *argv[])
{
	mongoc_client_pool_t *pool;
	mongoc_uri_t         *uri;
	pthread_t             threads[N_THREADS];
	
	mongoc_init ();
	
	uri = mongoc_uri_new ("mongodb://localhost/");
	// 創建客戶端池
	pool = mongoc_client_pool_new (uri);
	
	// 循環創建線程	
	for (i = 0; i < N_THREADS; i++) {
	   pthread_create (&threads[i], NULL, worker, pool);
	}
	// 等待各個線程結束
	for (i = 0; i < N_THREADS; i++) {
	   pthread_join (threads[i], NULL);
	}
	
	// 是否客戶端池
	mongoc_client_pool_destroy (pool);
	mongoc_uri_destroy (uri);
	mongoc_cleanup ();
	
	return 0;
}

7.今后的步驟

若要查找有關高級主題的信息,請瀏覽C Driver程序指南官方MongoDB文檔的其余部分.

常見的問題的幫助,請參閱疑難解答頁。要報告一個bug或請求一個新的功能,請按照這里的說明

更多的信息


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM