Nosql数据库之mongodb c++使用实例

mongodb是一个非关系型高速数据库,由多个文档(相当于关系数据库中的行记录)组成集合,多个集合(相当于关系数据库中的数据表)组成数据库。


使用命令安装或者源码安装mongodb,安装完成后mongod就是mongodb数据库服务的主程序了,指定参数或者配置文件启动命令如下

[cpp]  view plain  copy
  1. mongod -f /etc/mongodb.conf  
或者
[cpp]  view plain  copy
  1. mongod --dbpath=/home/lsx/mdata  
启动成功后可以在控制台看见一些ip和端口,数据存储目录的信息如下:

配置文件里面有ip,端口,数据存储目录的信息:

[cpp]  view plain  copy
  1. # mongodb.conf  
  2.   
  3. # Where to store the data.  
  4. dbpath=/var/lib/mongodb  
  5.   
  6. #where to log  
  7. logpath=/var/log/mongodb/mongodb.log  
  8.   
  9. logappend=true  
  10.   
  11. bind_ip = 127.0.0.1  
  12. #port = 27017  
  13.   
  14. # Enable journaling, http://www.mongodb.org/display/DOCS/Journaling  
  15. journal=true  
  16.   
  17. # Enables periodic logging of CPU utilization and I/O wait  
  18. #cpu = true  
  19.   
  20. # Turn on/off security.  Off is currently the default  
  21. #noauth = true  
  22. #auth = true  
  23.   
  24. # Verbose logging output.  
  25. #verbose = true  
  26.   
  27. # Inspect all client data for validity on receipt (useful for  
  28. # developing drivers)  
  29. #objcheck = true  
  30.   
  31. # Enable db quota management  
  32. #quota = true  
  33.   
  34. # Set oplogging level where n is  
  35. #   0=off (default)  
  36. #   1=W  
  37. #   2=R  
  38. #   3=both  
  39. #   7=W+some reads  
  40. #oplog = 0  
  41.   
  42. # Diagnostic/debugging option  
  43. #nocursors = true  
  44.   
  45. # Ignore query hints  
  46. #nohints = true  
  47.   
  48. # Disable the HTTP interface (Defaults to localhost:27018).  
  49. #nohttpinterface = true  
  50.   
  51. # Turns off server-side scripting.  This will result in greatly limited  
  52. # functionality  
  53. #noscripting = true  
  54.   
  55. # Turns off table scans.  Any query that would do a table scan fails.  
  56. #notablescan = true  
  57.   
  58. # Disable data file preallocation.  
  59. #noprealloc = true  
  60.   
  61. # Specify .ns file size for new databases.  
  62. # nssize = <size>  
  63.   
  64. # Accout token for Mongo monitoring server.  
  65. #mms-token = <token>  
  66.   
  67. # Server name for Mongo monitoring server.  
  68. #mms-name = <server-name>  
  69.   
  70. # Ping interval for Mongo monitoring server.  
  71. #mms-interval = <seconds>  
  72.   
  73. # Replication Options  
  74.   
  75. # in replicated mongo databases, specify here whether this is a slave or master  
  76. #slave = true  
  77. #source = master.example.com  
  78. # Slave only: specify a single database to replicate  
  79. #only = master.example.com  
  80. # or  
  81. #master = true  
  82. #source = slave.example.com  
  83.   
  84. # Address of a server to pair with.  
  85. #pairwith = <server:port>  
  86. # Address of arbiter server.  
  87. #arbiter = <server:port>  
  88. # Automatically resync if slave data is stale  
  89. #autoresync  
  90. # Custom size for replication operation log.  
  91. #oplogSize = <MB>  
  92. # Size limit for in-memory storage of op ids.  
  93. #opIdMem = <bytes>  
  94.   
  95. # SSL options  
  96. # Enable SSL on normal ports  
  97. #sslOnNormalPorts = true  
  98. # SSL Key file and password  
  99. #sslPEMKeyFile = /etc/ssl/mongodb.pem  
  100. #sslPEMKeyPassword = pass  

下载一个mongodb的c语言开发驱动包mongo-c-driver-1.3.5,写一个测试程序,插入一个文档

[cpp]  view plain  copy
  1. /* gcc example.c -o example $(pkg-config --cflags --libs libmongoc-1.0) */  
  2.   
  3. /* ./example-client [CONNECTION_STRING [COLLECTION_NAME]] */  
  4.   
  5. #include <mongoc.h>  
  6. #include <stdio.h>  
  7. #include <stdlib.h>  
  8. #include <string.h>  
  9.   
  10. #include "bson-types.h"  
  11. int main (int argc, char *argv[])  
  12. {  
  13.    mongoc_client_t *client;  
  14.    mongoc_collection_t *collection;  
  15.    mongoc_cursor_t *cursor;  
  16.    bson_error_t error;  
  17.    const bson_t *doc;  
  18.    const char *uristr = "mongodb://192.168.2.41/";  
  19.    const char *collection_name = "my_coll";  
  20.    bson_t query;  
  21.    char *str;  
  22.   
  23.    mongoc_init ();  
  24.   
  25.    if (argc > 1) {  
  26.       uristr = argv [1];  
  27.    }  
  28.   
  29.    if (argc > 2) {  
  30.       collection_name = argv [2];  
  31.    }  
  32.   
  33.    client = mongoc_client_new (uristr);  
  34.   
  35.    if (!client) {  
  36.       fprintf (stderr, "Failed to parse URI.\n");  
  37.       return EXIT_FAILURE;  
  38.    }  
  39.   
  40.    bson_init (&query);  
  41.   
  42. #if 0  
  43.    bson_append_utf8 (&query, "hello", -1, "world", -1);  
  44. #endif  
  45.   
  46.    collection = mongoc_client_get_collection (client, "hfrz", collection_name);  
  47.    /  
  48.    bson_t *doc2 = bson_new();  
  49.    BSON_APPEND_INT64(doc2, "id", 1);  
  50.    BSON_APPEND_INT64(doc2, "field1", 0);  
  51.    const char *msg = "test message";  
  52.    BSON_APPEND_BINARY(doc2, "field2", BSON_SUBTYPE_BINARY, (const uint8_t*)(msg), (uint32_t)(strlen(msg)+1));  
  53.    BSON_APPEND_UTF8 (doc2, "hello""world");  
  54.    bool r = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, doc2, NULL, &error);  
  55.    if (!r)  
  56.        printf("Insert Failure:%s\n", error.message);  
  57.    /  
  58.   
  59.    cursor = mongoc_collection_find (collection,  
  60.                                     MONGOC_QUERY_NONE,  
  61.                                     0,  
  62.                                     0,  
  63.                                     0,  
  64.                                     &query,  
  65.                                     NULL,  /* Fields, NULL for all. */  
  66.                                     NULL); /* Read Prefs, NULL for default */  
  67.   
  68.    while (mongoc_cursor_next (cursor, &doc)) {  
  69.       str = bson_as_json (doc, NULL);  
  70.       fprintf (stdout, "%s\n", str);  
  71.       bson_free (str);  
  72.    }  
  73.   
  74.    if (mongoc_cursor_error (cursor, &error)) {  
  75.       fprintf (stderr, "Cursor Failure: %s\n", error.message);  
  76.       return EXIT_FAILURE;  
  77.    }  
  78.   
  79.    bson_destroy (&query);  
  80.    mongoc_cursor_destroy (cursor);  
  81.    mongoc_collection_destroy (collection);  
  82.    mongoc_client_destroy (client);  
  83.   
  84.    mongoc_cleanup ();  
  85.   
  86.    return EXIT_SUCCESS;  
  87. }  

[cpp]  view plain  copy
  1. example:example-client.c  
  2.     g++ example-client.c -o example -g -I/usr/local/include/libmongoc-1.0 -I/usr/local/include/libbson-1.0 -L/usr/local/lib -lmongoc-1.0 -lbson-1.0  
  3. #$(pkg-config --cflags --libs libmongoc-1.0)  
  4. clean:  
  5.     rm -f example  


mongodb有很多的可视化工具,使用nosql manager for mongodb就可以在界面上看见刚才插入的文档了,如下图:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值