• mongodb C/C++ driver 通过帐号验证登录mongo服务器并进行相应操作


    转载:http://blog.chinaunix.net/uid-29454152-id-5587541.html

    1。C 语言登录mongodb,解决登录失败错误:Authentication failed.: mongoc client_authenticate error

    代码如下:

    点击(此处)折叠或打开

    1. #include <bson.h>
    2. #include <bcon.h>
    3. #include <mongoc.h>
    4. int main (int argc,
    5.       char *argv[])
    6. {
    7.      mongoc_client_t *client;
    8.      mongoc_database_t *database;
    9.      mongoc_collection_t *collection;
    10.      bson_t *command, reply, *insert;
    11.      bson_error_t error;
    12.      char *str;
    13.      bool retval;
    14.         
    15.      mongoc_init ();
    16.      client = mongoc_client_new("mongodb://mydbUser:aaaaaa@localhost:27017/?authSource=mydb");
    17.      database = mongoc_client_get_database (client, "mydb");
    18.      collection = mongoc_client_get_collection (client, "mydb", "student");
    19.      command = BCON_NEW ("ping", BCON_INT32 (1));
    20.      retval = mongoc_client_command_simple (client, "mydbUser", command, NULL, &reply, &error);
    21.      if (!retval) {
    22.          fprintf (stderr, "%s ", error.message);
    23.          return EXIT_FAILURE;
    24.      }
    25.      str = bson_as_json (&reply, NULL);
    26.      printf ("%s ", str);
    27.      insert = BCON_NEW ("hello", BCON_UTF8 ("world"));
    28.      if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, insert, NULL, &error)) {
    29.          fprintf (stderr, "%s ", error.message);
    30.      }
    31.      bson_destroy (insert);
    32.      bson_destroy (&reply);
    33.      bson_destroy (command);
    34.      bson_free (str);
    35.      mongoc_collection_destroy (collection);
    36.      mongoc_database_destroy (database);
    37.      mongoc_client_destroy (client);
    38.      mongoc_cleanup ();
    39.      return 0;
    40. }

    代码中第18行是登录关键:

    点击(此处)折叠或打开

    1. client = mongoc_client_new("mongodb://mydbUser:aaaaaa@localhost:27017/?authSource=mydb")

    容易看出登录是已uri格式登录的,并可划分成几部分:

    点击(此处)折叠或打开

    1. mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

    开始我的登录出错是因为uri格式不对,错误提示:

    点击(此处)折叠或打开

    1. what(): Authentication failed.: mongoc client_authenticate error

    2。C++ 登录mongodb

    代码如下:

    点击(此处)折叠或打开

    1. #include <iostream>
    2. #include <bsoncxx/builder/stream/document.hpp>
    3. #include <bsoncxx/json.hpp>
    4. #include <mongocxx/client.hpp>
    5. #include <mongocxx/instance.hpp>
    6. int main(int, char**) {
    7.     mongocxx::instance inst{};
    8.     mongocxx::client conn{mongocxx::uri{"mongodb://mydbUser:aaaaaa@localhost:27017/?authSource=mydb"}};
    9.     bsoncxx::builder::stream::document document{};
    10.     auto collection = conn["mydb"]["student"];
    11.     document << "hai!!!" << "world!!!";
    12.     collection.insert_one(document.view());
    13.     auto cursor = collection.find({});
    14.     for (auto&& doc : cursor) {
    15.         std::cout << bsoncxx::to_json(doc) << std::endl;
    16.     }
    17. }

    登录成功关键代码是第11行:

    点击(此处)折叠或打开

    1. mongocxx::client conn{mongocxx::uri{"mongodb://mydbUser:aaaaaa@localhost:27017/?authSource=mydb"}}

    说明同C 语言!!

    参考链接:

    C语言参考:
    C语言参考2:
    C++参考:(New-Driver)
    登录细节参考:
    登录细节参考2: http://api.mongodb.org/c/current/authentication.html

  • 相关阅读:
    LeetCode 234. 回文链表
    LeetCode 237. 删除链表中的节点
    LeetCode 20. 有效的括号( 括号配对 )
    堆栈操作合法性
    堆排序
    最大堆
    快速排序
    Bzoj1497 [NOI2006]最大获利
    Bzoj1001 [BeiJing2006]狼抓兔子
    Bzoj2716 [Violet 3]天使玩偶
  • 原文地址:https://www.cnblogs.com/liweikuan/p/14456226.html
Copyright © 2020-2023  润新知