std::string CjsonExporter::toCjson(const mongo::BSONObj &mongoChemObj) { // Follow the database link and convert to CJSON. MongoDatabase *db = MongoDatabase::instance(); if (!db) return ""; mongo::BSONObj structure = mongoChemObj.getObjectField("3dStructure"); if (!structure.hasField("$ref") || !structure.hasField("$id") || !structure.getField("$id").isSimpleType()) { return ""; } std::auto_ptr<mongo::DBClientCursor> cursor = db->query(db->databaseName() + "." + structure.getStringField("$ref"), QUERY("_id" << structure.getField("$id").OID()), 1); mongo::BSONObj object; if (cursor->more()) object = cursor->next(); else return ""; std::vector<std::string> toCopy; toCopy.push_back("name"); toCopy.push_back("inchi"); toCopy.push_back("formula"); toCopy.push_back("properties"); mongo::BSONObjBuilder builder; for (size_t i = 0; i < toCopy.size(); i++) { mongo::BSONElement field = mongoChemObj.getField(toCopy[i]); if (!field.eoo()) builder.append(field); } toCopy.clear(); toCopy.push_back("atoms"); toCopy.push_back("bonds"); for (size_t i = 0; i < toCopy.size(); i++) { mongo::BSONElement field = object.getField(toCopy[i]); if (!field.eoo()) builder.append(field); } // Add the chemical JSON version field. builder.append("chemical json", 0); mongo::BSONObj obj = builder.obj(); return obj.jsonString(mongo::Strict); }
void ComputationalResultsModel::setQuery(const mongo::Query &query) { beginInsertRows(QModelIndex(), 0, rowCount(QModelIndex())); m_objects.clear(); MongoDatabase *db = MongoDatabase::instance(); try { std::string collection = db->databaseName(); std::auto_ptr<mongo::DBClientCursor> cursor = db->connection()->query(collection + ".quantum", query); // Let's read in the results, but limit to 1000 in case of bad queries. int i(0); while (cursor->more() && ++i < 1000) m_objects.push_back(cursor->next().copy()); } catch (mongo::SocketException &e) { std::cerr << "Failed to query MongoDB: " << e.what() << std::endl; } endInsertRows(); }