TBson TBson::toBson(const QStringList &lst) { TBson ret; for (auto &str : lst) { bool res = appendBsonValue((bson_t *)ret.data(), qPrintable(str), 1); if (!res) break; } return ret; }
QVariantMap TMongoDriver::findOne(const QString &ns, const QVariantMap &criteria, const QStringList &fields) { TBson bs; mongo_clear_errors(mongoConnection); int status = mongo_find_one(mongoConnection, qPrintable(ns), (bson *)TBson::toBson(criteria).data(), (bson *)TBson::toBson(fields).data(), (bson *)bs.data()); if (status != MONGO_OK) { tSystemError("MongoDB Error: %s", mongoConnection->lasterrstr); return QVariantMap(); } return TBson::fromBson(bs); }
TBson TBson::toBson(const QVariantMap &query, const QVariantMap &orderBy) { TBson ret; bson_t child; // query clause BSON_APPEND_DOCUMENT_BEGIN((bson_t *)ret.data(), "$query", &child); appendBson(&child, query); bson_append_document_end((bson_t *)ret.data(), &child); // orderBy clause if (!orderBy.isEmpty()) { BSON_APPEND_DOCUMENT_BEGIN((bson_t *)ret.data(), "$orderby", &child); appendBson(&child, orderBy); bson_append_document_end((bson_t *)ret.data(), &child); } return ret; }
QVariantMap TBson::fromBson(const TBson &bson) { return TBson::fromBson(bson.constData()); }
TBson TBson::toBson(const QVariantMap &map) { TBson ret; appendBson(ret.data(), map); return ret; }