Nosql数据库之mongodb c++使用实例
生活随笔
收集整理的這篇文章主要介紹了
Nosql数据库之mongodb c++使用实例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
mongodb是一個非關系型高速數(shù)據(jù)庫,由多個文檔(相當于關系數(shù)據(jù)庫中的行記錄)組成集合,多個集合(相當于關系數(shù)據(jù)庫中的數(shù)據(jù)表)組成數(shù)據(jù)庫。
使用命令安裝或者源碼安裝mongodb,安裝完成后mongod就是mongodb數(shù)據(jù)庫服務的主程序了,指定參數(shù)或者配置文件啟動命令如下
mongod --dbpath=/home/lsx/mdata啟動成功后可以在控制臺看見一些ip和端口,數(shù)據(jù)存儲目錄的信息如下:
配置文件里面有ip,端口,數(shù)據(jù)存儲目錄的信息:
# mongodb.conf# Where to store the data. dbpath=/var/lib/mongodb#where to log logpath=/var/log/mongodb/mongodb.loglogappend=truebind_ip = 127.0.0.1 #port = 27017# Enable journaling, http://www.mongodb.org/display/DOCS/Journaling journal=true# Enables periodic logging of CPU utilization and I/O wait #cpu = true# Turn on/off security. Off is currently the default #noauth = true #auth = true# Verbose logging output. #verbose = true# Inspect all client data for validity on receipt (useful for # developing drivers) #objcheck = true# Enable db quota management #quota = true# Set oplogging level where n is # 0=off (default) # 1=W # 2=R # 3=both # 7=W+some reads #oplog = 0# Diagnostic/debugging option #nocursors = true# Ignore query hints #nohints = true# Disable the HTTP interface (Defaults to localhost:27018). #nohttpinterface = true# Turns off server-side scripting. This will result in greatly limited # functionality #noscripting = true# Turns off table scans. Any query that would do a table scan fails. #notablescan = true# Disable data file preallocation. #noprealloc = true# Specify .ns file size for new databases. # nssize = <size># Accout token for Mongo monitoring server. #mms-token = <token># Server name for Mongo monitoring server. #mms-name = <server-name># Ping interval for Mongo monitoring server. #mms-interval = <seconds># Replication Options# in replicated mongo databases, specify here whether this is a slave or master #slave = true #source = master.example.com # Slave only: specify a single database to replicate #only = master.example.com # or #master = true #source = slave.example.com# Address of a server to pair with. #pairwith = <server:port> # Address of arbiter server. #arbiter = <server:port> # Automatically resync if slave data is stale #autoresync # Custom size for replication operation log. #oplogSize = <MB> # Size limit for in-memory storage of op ids. #opIdMem = <bytes># SSL options # Enable SSL on normal ports #sslOnNormalPorts = true # SSL Key file and password #sslPEMKeyFile = /etc/ssl/mongodb.pem #sslPEMKeyPassword = pass下載一個mongodb的c語言開發(fā)驅動包mongo-c-driver-1.3.5,寫一個測試程序,插入一個文檔 /* gcc example.c -o example $(pkg-config --cflags --libs libmongoc-1.0) *//* ./example-client [CONNECTION_STRING [COLLECTION_NAME]] */#include <mongoc.h> #include <stdio.h> #include <stdlib.h> #include <string.h>#include "bson-types.h" int main (int argc, char *argv[]) {mongoc_client_t *client;mongoc_collection_t *collection;mongoc_cursor_t *cursor;bson_error_t error;const bson_t *doc;const char *uristr = "mongodb://192.168.2.41/";const char *collection_name = "my_coll";bson_t query;char *str;mongoc_init ();if (argc > 1) {uristr = argv [1];}if (argc > 2) {collection_name = argv [2];}client = mongoc_client_new (uristr);if (!client) {fprintf (stderr, "Failed to parse URI.\n");return EXIT_FAILURE;}bson_init (&query);#if 0bson_append_utf8 (&query, "hello", -1, "world", -1); #endifcollection = mongoc_client_get_collection (client, "hfrz", collection_name);/bson_t *doc2 = bson_new();BSON_APPEND_INT64(doc2, "id", 1);BSON_APPEND_INT64(doc2, "field1", 0);const char *msg = "test message";BSON_APPEND_BINARY(doc2, "field2", BSON_SUBTYPE_BINARY, (const uint8_t*)(msg), (uint32_t)(strlen(msg)+1));BSON_APPEND_UTF8 (doc2, "hello", "world");bool r = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, doc2, NULL, &error);if (!r)printf("Insert Failure:%s\n", error.message);/cursor = mongoc_collection_find (collection,MONGOC_QUERY_NONE,0,0,0,&query,NULL, /* Fields, NULL for all. */NULL); /* Read Prefs, NULL for default */while (mongoc_cursor_next (cursor, &doc)) {str = bson_as_json (doc, NULL);fprintf (stdout, "%s\n", str);bson_free (str);}if (mongoc_cursor_error (cursor, &error)) {fprintf (stderr, "Cursor Failure: %s\n", error.message);return EXIT_FAILURE;}bson_destroy (&query);mongoc_cursor_destroy (cursor);mongoc_collection_destroy (collection);mongoc_client_destroy (client);mongoc_cleanup ();return EXIT_SUCCESS; }
example:example-client.cg++ 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 #$(pkg-config --cflags --libs libmongoc-1.0) clean:rm -f example
mongodb有很多的可視化工具,使用nosql manager for mongodb就可以在界面上看見剛才插入的文檔了,如下圖:
總結
以上是生活随笔為你收集整理的Nosql数据库之mongodb c++使用实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高速缓存系统之memcache c++使
- 下一篇: boost之内存池使用实例