我正在开发一个应用程序,它以高频率(数千个文档亚秒)将数据插入MongoDB。因此,索引和存储空间优化是关键。
因此,在插入第一条记录(集合名称是动态的)之前,我想用C ++驱动程序执行以下操作:
conn.ensureIndex(coll, mongo::fromjson("{'_id.o':1}"));
然后我的插入命令conn.insert(coll, vec);
显然适用于任意数量的向量元素。
非常感谢帮助!
答案 0 :(得分:1)
我不确定我理解为什么要删除_id
索引并将其替换为另一个索引,但仍设置_id
字段。
_id
(documentation)类扩展方法createCollection
来禁用集合的DbClientWithCommands
。当然,您还需要确保驱动程序不会自动插入_id
(许多驱动程序对此,所以对于某些人来说,这仍然是一个问题)。 ensureIndex
有一个background
参数,您可以提供documentation)要创建没有_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);
}