Reputation: 253
I am developing an application which inserts data into MongoDB in high frequency (thousands of documents sub-second). As such, index and storage space optimization is key.
So, before inserting the first record (collection names are dynamic), I would like to do the following with the C++ driver:
conn.ensureIndex(coll, mongo::fromjson("{'_id.o':1}"));
My insert command is then conn.insert(coll, vec);
which obviously works fine for any number of vector elements.
Help is greatly appreciated!
Upvotes: 2
Views: 555
Reputation: 59763
I'm not sure I understand why you're removing the _id
index and replacing it with another index, yet still setting the _id
field.
_id
for a collection if needed, by extending the method createCollection
from the DbClientWithCommands
(documentation) class. Of course, you need to also make sure that an _id
is not automatically inserted by the driver automatically (many drivers to this, so for some people, this will still be an issue). ensureIndex
has a background
parameter you can provide (documentation)For creating a collection without an _id
and using autoIndexId
, you'd need to create a new function, as the built-in one currently does, you'd need to copy the code as mentioned above and do this:
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);
}
Upvotes: 1