diff options
Diffstat (limited to 'src/mongo/db/query')
-rw-r--r-- | src/mongo/db/query/cursor_response.cpp | 26 | ||||
-rw-r--r-- | src/mongo/db/query/cursor_response.h | 6 |
2 files changed, 32 insertions, 0 deletions
diff --git a/src/mongo/db/query/cursor_response.cpp b/src/mongo/db/query/cursor_response.cpp index 14b8af5e8f6..da7ef1127bc 100644 --- a/src/mongo/db/query/cursor_response.cpp +++ b/src/mongo/db/query/cursor_response.cpp @@ -39,6 +39,7 @@ namespace mongo { namespace { +const char kCursorsField[] = "cursors"; const char kCursorField[] = "cursor"; const char kIdField[] = "id"; const char kNsField[] = "ns"; @@ -129,6 +130,31 @@ CursorResponse::CursorResponse(NamespaceString nss, _latestOplogTimestamp(latestOplogTimestamp), _writeConcernError(std::move(writeConcernError)) {} +std::vector<StatusWith<CursorResponse>> CursorResponse::parseFromBSONMany( + const BSONObj& cmdResponse) { + std::vector<StatusWith<CursorResponse>> cursors; + BSONElement cursorsElt = cmdResponse[kCursorsField]; + + // If there is not "cursors" array then treat it as a single cursor response + if (cursorsElt.type() != BSONType::Array) { + cursors.push_back(parseFromBSON(cmdResponse)); + } else { + BSONObj cursorsObj = cursorsElt.embeddedObject(); + for (BSONElement elt : cursorsObj) { + if (elt.type() != BSONType::Object) { + cursors.push_back({ErrorCodes::BadValue, + str::stream() + << "Cursors array element contains non-object element: " + << elt}); + } else { + cursors.push_back(parseFromBSON(elt.Obj())); + } + } + } + + return cursors; +} + StatusWith<CursorResponse> CursorResponse::parseFromBSON(const BSONObj& cmdResponse) { Status cmdStatus = getStatusFromCommandResult(cmdResponse); if (!cmdStatus.isOK()) { diff --git a/src/mongo/db/query/cursor_response.h b/src/mongo/db/query/cursor_response.h index 529654118df..52f4d30ed6a 100644 --- a/src/mongo/db/query/cursor_response.h +++ b/src/mongo/db/query/cursor_response.h @@ -166,6 +166,12 @@ public: }; /** + * Constructs a vector of CursorResponses from a command BSON response that represents one or + * more cursors. + */ + static std::vector<StatusWith<CursorResponse>> parseFromBSONMany(const BSONObj& cmdResponse); + + /** * Constructs a CursorResponse from the command BSON response. */ static StatusWith<CursorResponse> parseFromBSON(const BSONObj& cmdResponse); |