void MongoClient::saveDocument(const mongo::BSONObj &obj, const MongoNamespace &ns) { mongo::BSONElement id = obj.getField("_id"); mongo::BSONObjBuilder builder; builder.append(id); mongo::BSONObj bsonQuery = builder.obj(); mongo::Query query(bsonQuery); _dbclient->update(ns.toString(), query, obj, true, false); checkLastErrorAndThrow(ns.databaseName()); }
void MongoClient::copyCollectionToDiffServer(mongo::DBClientConnection *const fromServ,const MongoNamespace &from, const MongoNamespace &to) { if (!_dbclient->exists(to.toString())) _dbclient->createCollection(to.toString()); std::auto_ptr<mongo::DBClientCursor> cursor(fromServ->query(from.toString(), mongo::Query())); while (cursor->more()) { mongo::BSONObj bsonObj = cursor->next(); _dbclient->insert(to.toString(), bsonObj); } }
void MongoClient::copyCollectionToDiffServer(mongo::DBClientBase *const fromServ, const MongoNamespace &from, const MongoNamespace &to) { if (!_dbclient->exists(to.toString())) _dbclient->createCollection(to.toString()); std::unique_ptr<mongo::DBClientCursor> cursor(fromServ->query(from.toString(), mongo::Query())); // Cursor may be NULL, it means we have connectivity problem if (!cursor) throw mongo::DBException("Network error while attempting to run query", 0); while (cursor->more()) { mongo::BSONObj bsonObj = cursor->next(); _dbclient->insert(to.toString(), bsonObj); } }
void MongoClient::saveDocument(const mongo::BSONObj &obj, const MongoNamespace &ns) { mongo::BSONElement id = obj.getField("_id"); mongo::BSONObjBuilder builder; builder.append(id); mongo::BSONObj bsonQuery = builder.obj(); mongo::Query query(bsonQuery); _dbclient->update(ns.toString(), query, obj, true, false); //_dbclient->save(ns.toString().toStdString(), obj); }
void MongoClient::renameCollection(const MongoNamespace &ns, const std::string &newCollectionName) { MongoNamespace from(ns); MongoNamespace to(ns.databaseName(), newCollectionName); // Building { renameCollection: <source-namespace>, to: <target-namespace> } mongo::BSONObjBuilder command; // { collStats: "db.collection", scale : 1 } command.append("renameCollection", from.toString()); command.append("to", to.toString()); mongo::BSONObj result; _dbclient->runCommand("admin", command.obj(), result); // this command should be run against "admin" db }
void MongoClient::duplicateCollection(const MongoNamespace &ns, const std::string &newCollectionName) { MongoNamespace from(ns); MongoNamespace to(ns.databaseName(), newCollectionName); if (!_dbclient->exists(to.toString())) _dbclient->createCollection(to.toString()); std::auto_ptr<mongo::DBClientCursor> cursor(_dbclient->query(from.toString(), mongo::Query())); while (cursor->more()) { mongo::BSONObj bsonObj = cursor->next(); _dbclient->insert(to.toString(), bsonObj); } }
void MongoClient::duplicateCollection(const MongoNamespace &ns, const std::string &newCollectionName) { MongoNamespace from(ns); MongoNamespace to(ns.databaseName(), newCollectionName); if (!_dbclient->exists(to.toString())) _dbclient->createCollection(to.toString()); std::unique_ptr<mongo::DBClientCursor> cursor(_dbclient->query(from.toString(), mongo::Query())); // Cursor may be NULL, it means we have connectivity problem if (!cursor) throw mongo::DBException("Network error while attempting to run query", 0); while (cursor->more()) { mongo::BSONObj bsonObj = cursor->next(); _dbclient->insert(to.toString(), bsonObj); } }
void MongoClient::removeDocuments(const MongoNamespace &ns, mongo::Query query, bool justOne /*= true*/) { _dbclient->remove(ns.toString(), query, justOne); }
void MongoClient::insertDocument(const mongo::BSONObj &obj, const MongoNamespace &ns) { _dbclient->insert(ns.toString(), obj); }
void MongoClient::dropCollection(const MongoNamespace &ns) { _dbclient->dropCollection(ns.toString()); }
void MongoClient::createCollection(const MongoNamespace &ns) { _dbclient->createCollection(ns.toString()); }
void MongoClient::removeDocuments(const MongoNamespace &ns, mongo::Query query, bool justOne /*= true*/) { _dbclient->remove(ns.toString(), query, justOne); checkLastErrorAndThrow(ns.databaseName()); }
void MongoClient::insertDocument(const mongo::BSONObj &obj, const MongoNamespace &ns) { _dbclient->insert(ns.toString(), obj); checkLastErrorAndThrow(ns.databaseName()); }