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

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\n", error.message);
  23.          return EXIT_FAILURE;
  24.      }
  25.      str = bson_as_json (&reply, NULL);
  26.      printf ("%s\n", 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\n", 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语言参考:http://api.mongodb.org/c/1.2.0/tutorial.html#connecting
C语言参考2:http://api.mongodb.org/c/1.2.0/
C++参考:https://github.com/mongodb/mongo-cxx-driver/wiki/Quickstart-Guide-(New-Driver)
登录细节参考:https://docs.mongodb.org/manual/reference/connection-string/
登录细节参考2: http://api.mongodb.org/c/current/authentication.html

猜你喜欢

转载自blog.csdn.net/warrior_zhang/article/details/50503405