读取MongoDB ObjectID并转换为string

c++代码为:

boost::shared_ptr LoginService::Login(const std::string& email, const std::string& pwd) {
  boost::shared_ptr login_page(new LoginPage);
  mongo::BSONObjBuilder condition;
  condition.append("email", email);
  condition.append("pwd", pwd);
  mongo::Query query(condition.obj());
  
  boost::shared_ptr session = MongoSessionFactory::GetSession();
  std::string db_name =  MyApp::Instance().get_config().mongo_db_name;
  std::unique_ptr cursor = session->get().query("ide_site.login", query, 1);
  if (cursor->more()) {
    mongo::BSONObj record = cursor->next();
    mongo::BSONElement e = record.getField("_id");
    if (record.getObjectID(e)) {
      mongo::OID oid = e.__oid();
      login_page->oid = oid.toString(); 
    }     
  }
  return login_page;
}

如果mongodb存储的是 { "_id" : ObjectId("5420013272fe096c39901048"), "email" : "laoro@xx.com", "pwd" : "123456" },

则上面的login_page->oid 就为5420013272fe096c39901048.

你可能感兴趣的:(C++,JSON,MongoDB,CppCMS)