std::vector<MongoCollectionInfo> MongoClient::runCollStatsCommand(const std::vector<std::string> &namespaces) { std::vector<MongoCollectionInfo> infos; for (std::vector<std::string>::const_iterator it = namespaces.begin(); it!=namespaces.end(); ++it) { MongoCollectionInfo info = runCollStatsCommand(*it); if (info.ns().isValid()){ infos.push_back(info); } } return infos; }
std::vector<EnsureIndexInfo> MongoClient::getIndexes(const MongoCollectionInfo &collection) const { std::vector<EnsureIndexInfo> result; std::string indexNamespace = mongo::Namespace(collection.ns().toString()).getSisterNS("system.indexes"); std::auto_ptr<mongo::DBClientCursor> cursor(_dbclient-> query( indexNamespace.c_str(), BSON("ns" << collection.ns().toString()), 0, 0, 0, mongo::QueryOption_SlaveOk, 0) ); while (cursor->more()) { mongo::BSONObj bsonObj = cursor->next(); result.push_back(makeEnsureIndexInfoFromBsonObj(collection,bsonObj)); } return result; }
void MongoClient::renameIndexFromCollection(const MongoCollectionInfo &collection, const std::string &oldIndexName, const std::string &newIndexName) const { // This is simply an example of how to perform modifications of // BSON objects. Because BSONObj is immutable, you need to create // copy of this object, using BSONObjBuilder and BSONObjIterator. // // But we need to do not just simple renaming of Index name, we // also should allow our users to fully modify Index // (i.e. change name, keys, unique flag, sparse flag etc.) // // This should be done using the same dialog as for "Add Index". MongoNamespace ns(collection.ns().databaseName(), "system.indexes"); std::string systemIndexesNs = ns.toString(); // Building this JSON: { "name" : "oldIndexName" } mongo::BSONObj query(mongo::BSONObjBuilder() .append("name", oldIndexName) .obj()); // Searching for index with "oldIndexName" // with this query: db.system.indexes.find({ name : "oldIndexName"} mongo::BSONObj indexBson = _dbclient->findOne(systemIndexesNs, mongo::Query(query)); if (indexBson.isEmpty()) return; // Here we are building copy of "indexBson" object and // changing "name" field's value from "oldIndexText" to "newIndexText": mongo::BSONObjBuilder builder; mongo::BSONObjIterator i(indexBson); while (i.more()) { mongo::BSONElement element = i.next(); if (mongo::StringData(element.fieldName()).compare("name") == 0) { builder.append("name", newIndexName); continue; } builder.append(element); } std::string collectionNs = collection.ns().toString(); _dbclient->dropIndex(collectionNs, oldIndexName); _dbclient->insert(systemIndexesNs, builder.obj()); }
std::vector<EnsureIndexInfo> MongoClient::getIndexes(const MongoCollectionInfo &collection) const { std::vector<EnsureIndexInfo> result; std::auto_ptr<mongo::DBClientCursor> cursor(_dbclient->getIndexes(collection.ns().toString())); while (cursor->more()) { mongo::BSONObj bsonObj = cursor->next(); result.push_back(makeEnsureIndexInfoFromBsonObj(collection,bsonObj)); } return result; }
std::vector<EnsureIndexInfo> MongoClient::getIndexes(const MongoCollectionInfo &collection) const { std::vector<EnsureIndexInfo> result; std::list<mongo::BSONObj> indexes = _dbclient->getIndexSpecs(collection.ns().toString()); // std::unique_ptr<mongo::DBClientCursor> cursor(_dbclient->getIndexSpecs(collection.ns().toString())); for (std::list<mongo::BSONObj>::iterator it = indexes.begin(); it != indexes.end(); ++it) { mongo::BSONObj bsonObj = *it; result.push_back(makeEnsureIndexInfoFromBsonObj(collection, bsonObj)); } return result; }
void MongoClient::dropIndexFromCollection(const MongoCollectionInfo &collection, const std::string &indexName) const { _dbclient->dropIndex(collection.ns().toString(), indexName); }