我可以通过 mongodb shell 在 mongodb 中创建索引
即 db['test'].ensureIndex( { fieldname:1 } )
但是如何使用 mongo-c-driver 创建相同的索引呢? 谁能指点我一下?非常感谢!
最佳答案
如果您有一组键为 name
和 age
的文档
您可以像这样创建索引:
static void tutorial_index( mongo_connection *conn ) {
bson key[1];
bson_init( key );
bson_append_int( key, "name", 1 );
bson_finish( key );
mongo_create_index( conn, "tutorial.persons", key, 0, NULL );
bson_destroy( key );
printf( "simple index created on \"name\"\n" );
bson_init( key );
bson_append_int( key, "age", 1 );
bson_append_int( key, "name", 1 );
bson_finish( key );
mongo_create_index( conn, "tutorial.persons", key, 0, NULL );
bson_destroy( key );
printf( "compound index created on \"age\", \"name\"\n" );
}
关于使用mongodb C驱动创建索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32245715/