日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

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

發(fā)布時(shí)間:2025/3/21 c/c++ 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Nosql数据库之mongodb c++使用实例 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

mongodb是一個(gè)非關(guān)系型高速數(shù)據(jù)庫,由多個(gè)文檔(相當(dāng)于關(guān)系數(shù)據(jù)庫中的行記錄)組成集合,多個(gè)集合(相當(dāng)于關(guān)系數(shù)據(jù)庫中的數(shù)據(jù)表)組成數(shù)據(jù)庫。


使用命令安裝或者源碼安裝mongodb,安裝完成后mongod就是mongodb數(shù)據(jù)庫服務(wù)的主程序了,指定參數(shù)或者配置文件啟動(dòng)命令如下

mongod -f /etc/mongodb.conf 或者
mongod --dbpath=/home/lsx/mdata啟動(dòng)成功后可以在控制臺(tái)看見一些ip和端口,數(shù)據(jù)存儲(chǔ)目錄的信息如下:

配置文件里面有ip,端口,數(shù)據(jù)存儲(chǔ)目錄的信息:

# 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
下載一個(gè)mongodb的c語言開發(fā)驅(qū)動(dòng)包mongo-c-driver-1.3.5,寫一個(gè)測(cè)試程序,插入一個(gè)文檔

/* 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就可以在界面上看見剛才插入的文檔了,如下圖:




《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的Nosql数据库之mongodb c++使用实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。