使用mongodb C驱动创建索引

标签 c mongodb indexing

我可以通过 mongodb shell 在 mongodb 中创建索引

db['test'].ensureIndex( { fieldname:1 } )

但是如何使用 mongo-c-driver 创建相同的索引呢? 谁能指点我一下?非常感谢!

最佳答案

如果您有一组键为 nameage 的文档

您可以像这样创建索引:

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 Reference

关于使用mongodb C驱动创建索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32245715/

相关文章:

mongodb - 我可以使用正则表达式删除 mongodb 的文档吗?

mongodb - mongocli 是否支持 Apple M1 芯片的公式 URL?

javascript - Mongo/Mongoose - markModified 不起作用

memory - 虚拟索引物理标记分页与虚拟索引和虚拟标记分页之间有什么区别?

mysql - 在 MYSQL 中索引一个位字段

c - 对如何完成计划感到不知所措

python - 如何向 IGMP 设备发送 UDP 数据报?

c - 大维度值错误 - 运行时检查失败 #2 - 变量 'mat' 周围的堆栈已损坏

sql - 在 PostgreSQL 中索引空值

c - BLOB,它是如何工作的