用于海量数据批量插入的C ++ MongoClient索引优化

时间:2013-10-31 09:08:57

标签: c++ mongodb indexing

我正在开发一个应用程序,它以高频率(数千个文档亚秒)将数据插入MongoDB。因此,索引和存储空间优化是关键。

因此,在插入第一条记录(集合名称是动态的)之前,我想用C ++驱动程序执行以下操作:

  • 关闭_id上的autoindex(我有一个suboc作为_id字段),不知道如何使用C ++驱动程序
  • 确保一个特殊索引,这适用于conn.ensureIndex(coll, mongo::fromjson("{'_id.o':1}"));
  • 将索引设置为后台(不知道如何使用C ++驱动程序)
  • 将填充设置为零(文档永远不会再次更新)不知道如何使用C ++驱动程序执行此操作

然后我的插入命令conn.insert(coll, vec);显然适用于任意数量的向量元素。

非常感谢帮助!

1 个答案:

答案 0 :(得分:1)

我不确定我理解为什么要删除_id索引并将其替换为另一个索引,但仍设置_id字段。

  1. 显然,如果需要,您可以通过从_iddocumentation)类扩展方法createCollection来禁用集合的DbClientWithCommands。当然,您还需要确保驱动程序不会自动插入_id(许多驱动程序对此,所以对于某些人来说,这仍然是一个问题)。
  2. 当前的驱动程序方法ensureIndex有一个background参数,您可以提供documentation
  3. 我不知道以编程方式控制填充的任何方法。随着时间的推移,MongoDB会自动确定一个集合。如果你不修改文档,我希望它接近1(意味着没有填充)。检查统计数据是否确定。
  4. 要创建没有_id并使用autoIndexId的集合,您需要创建一个新函数,就像内置函数一样,您需要将代码复制为如上所述并执行此操作:

    bool MyClass::createCollection(const string &ns, long long size, 
                                   bool capped, int max, bool disableAutoIndexId, BSONObj *info) {
        verify(!capped||size);
        BSONObj o;
        if ( info == 0 )    info = &o;
        BSONObjBuilder b;
        string db = nsToDatabase(ns);
        b.append("create", ns.c_str() + db.length() + 1);
        if ( size ) b.append("size", size);
        if ( capped ) b.append("capped", true);
        if ( max ) b.append("max", max);
        if ( disableAutoIndexId ) b.append("autoIndexId", false);
        return runCommand(db.c_str(), b.done(), *info);
    }