我正在用C编写MongoDB的GUI客户端。我使用C driver for MongoDB。我想获得一个数据库及其集合的列表,但我找不到documentation中的任何函数来执行此操作。
如何使用C驱动程序获取数据库和集合列表?
答案 0 :(得分:4)
这里的其他答案似乎有一半完整,甚至与C / C ++接口无关。经过深思熟虑后,这对我有用:
string serverHost("127.0.0.1:27017");
mongo::DBClientConnection conn;
string errmsg;
if( conn.connect( serverHost, errmsg ) )
{
BSONObj cmd = mongo::fromjson( "{listDatabases: 1}" );
BSONObj info;
if( conn.runCommand( "admin", cmd, info ) )
{
BSONElement arrayel = info.getField("databases");
std::vector<BSONElement> mdArray = arrayel.Array();
std::vector<BSONElement>::iterator iter;
for( iter=mdArray.begin(); iter!=mdArray.end(); ++iter )
{
BSONElement element = *iter;
BSONObj obj = element.Obj();
// HERE IS THE DATABASE NAME
string dbname = obj.getStringField("name");
// HERE IS THE LIST OF COLLECTIONS, BUT MAY NEED TO IGNORE ONE
// TITLED "system.indexes"
list<string> collNamespaces =
conn.getCollectionNames(dbname);
list<string>::iterator iter2 = collNamespaces.begin();
while( iter2 != collNamespaces.end() )
{
// EACH ENTRY HAS THE FULL NAMESPACE ("database:collection").
// Use this method to strip off the database name
string collectionName = mongo::nsGetCollection(*iter2);
++iter2;
} // END WHILE iterate through collections
} // END WHILE iterate through databases
} // END IF runCommand() returned success
} // END IF database connected
答案 1 :(得分:2)
我认为您必须使用mongo_run_command(mongo *conn, const char * db, bson *command, bson *out)
command
是BSON命令。您应该能够使用定义的命令here。请注意,某些命令要求在数据库admin
上发出它们。所以在mongodb shell中,可以像这样查询数据库列表:
> use admin
switched to db admin
> db.runCommand({listDatabases: 1})
{
"databases" : [
{
"name" : "geo",
"sizeOnDisk" : 14975762432,
"empty" : false
},
{
"name" : "local",
"sizeOnDisk" : 1,
"empty" : true
}
],
"totalSize" : 14975762432,
"ok" : 1
}
通过此操作的结果,您可以查询每个数据库以查找可用的集合。我没有尝试使用C接口,但我相信你应该能够通过调用mongo_run_command
并传递适当的参数来实现同样的目的。
答案 2 :(得分:1)
要获取数据库中的所有馆藏名称,您可以使用<?php
// get database connection
include_once 'config/database.php';
$database = new Database();
$db = $database->getConnection();
class Facturas{
// database connection and table name
private $conn;
private $table_name = "factura";
// object properties
public $id;
public $cliente;
public function __construct($db){
$this->conn = $db;
}
function create(){
$query = "INSERT INTO
" . $this->table_name . "
SET
cliente = :cliente";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(':cliente', $this->cliente);
if($stmt->execute()){
return true;
}else{
return false;
}
}
}
$factura = new facturas($db);
class DetalleFacturas{
// database connection and table name
private $conn;
private $table_name = "detallefactura";
// object properties
public $id;
public $fac_id;
public $tratamiento;
public function __construct($db){
$this->conn = $db;
}
function create(){
$query = "INSERT INTO
" . $this->table_name . "
SET
fac_id = :fac_id, tratamiento = :tratamiento";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(':fac_id', $fac_ids);
$stmt->bindParam(':tratamiento', $tratamientos);
if (is_array($this->tratamiento)) {
foreach ($this->tratamiento as $clave=> $tratamientos) {
$fac_ids = $this->fac_id[$clave];
$stmt->execute();
}
}
}
}
$detallefactura = new detallefacturas($db);
if ($_POST){
$factura->cliente = $_POST['cliente'];
$factura->create();
$last[] = $db->lastInsertId('factura');
$detallefactura->fac_id = $last;
$detallefactura->tratamiento = $_POST['tratamiento'];
$detallefactura->create();
} else {
echo "no va";
}
?>
<form action='prueba2.php' method='post'>
<table class='table table-hover table-responsive table-bordered'>
<tr>
<td>Cliente</td>
<td><input type='text' name='cliente' class='form-control'></td>
</tr>
<tr>
<td>Tratamiento</td>
<td><input type='text' name='tratamiento[]' class='form-control'></td>
</tr>
<tr>
<td>Tratamiento2</td>
<td><input type='text' name='tratamiento[]' class='form-control'></td>
</tr>
<tr>
<td>
<button type='submit' class='btn btn-primary'>Crear</button>
</td>
</tr>
</table>
</form>
<pre><?php print_r($detallefactura) ?></pre>
mongoc_database_get_collection_names
答案 3 :(得分:0)
Matlab驱动程序提供了两个功能: mongo_get_databases ()和: mongo_get_database_collections()(下面的示例代码段),它们似乎提供了您的功能要求:
EXPORT mxArray* mongo_get_databases(struct mongo_* conn) {
bson out;
mxArray* ret;
int count = 0;
bson_iterator it, databases, database;
int i = 0;
if (mongo_simple_int_command((mongo*)conn, "admin", "listDatabases", 1, &out) != MONGO_OK) {
bson_destroy(&out);
return 0;
}
bson_iterator_init(&it, &out);
...
bson_destroy(&out);
return ret;
}