Skip to content

Commit f127398

Browse files
committed
CSHARP-1145: added support for cursors in list collections and list indexes.
1 parent 71107b6 commit f127398

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

MongoDB.Driver/Operations/ListCollectionsOperation.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,28 @@ private IEnumerable<BsonDocument> ExecuteUsingCommand(MongoConnection connection
108108
var operation = new CommandOperation<CommandResult>(_databaseName, _readerSettings, _writerSettings, command, flags, options, _readPreference, serializationOptions, serializer);
109109
var result = operation.Execute(connection);
110110
var response = result.Response;
111-
return response["collections"].AsBsonArray.Select(value => (BsonDocument)value).ToList();
111+
var cursorDocument = response["cursor"];
112+
var cursorId = cursorDocument["id"].ToInt64();
113+
var firstBatch = cursorDocument["firstBatch"].AsBsonArray.Select(v => v.AsBsonDocument);
114+
var ns = cursorDocument["ns"].ToString();
115+
using (var enumerator = new CursorEnumerator<BsonDocument>(
116+
new ServerInstanceConnectionProvider(connection.ServerInstance),
117+
ns,
118+
firstBatch,
119+
cursorId,
120+
0,
121+
0,
122+
_readerSettings,
123+
BsonDocumentSerializer.Instance,
124+
null))
125+
{
126+
var collections = new List<BsonDocument>();
127+
while (enumerator.MoveNext())
128+
{
129+
collections.Add(enumerator.Current);
130+
}
131+
return collections;
132+
}
112133
}
113134

114135
private IEnumerable<BsonDocument> ExecuteUsingQuery(MongoConnection connection)

MongoDB.Driver/Operations/ListIndexesOperation.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public ListIndexesOperation(
4646
ReadPreference readPreference,
4747
BsonBinaryReaderSettings readerSettings,
4848
BsonBinaryWriterSettings writerSettings)
49-
{
49+
{
5050
if (string.IsNullOrEmpty(databaseName))
5151
{
5252
throw new ArgumentException("Database name cannot be null or empty.", "databaseName");
@@ -95,7 +95,10 @@ public IEnumerable<BsonDocument> Execute(MongoConnection connection)
9595

9696
private IEnumerable<BsonDocument> ExecuteUsingCommand(MongoConnection connection)
9797
{
98-
var command = new CommandDocument("listIndexes", _collectionName);
98+
var command = new CommandDocument
99+
{
100+
{ "listIndexes", _collectionName }
101+
};
99102
var flags = QueryFlags.None;
100103
var options = new BsonDocument();
101104
IBsonSerializationOptions serializationOptions = null;
@@ -118,7 +121,29 @@ private IEnumerable<BsonDocument> ExecuteUsingCommand(MongoConnection connection
118121
throw;
119122
}
120123

121-
return result.Response["indexes"].AsBsonArray.Cast<BsonDocument>();
124+
125+
var cursorDocument = result.Response["cursor"];
126+
var cursorId = cursorDocument["id"].ToInt64();
127+
var firstBatch = cursorDocument["firstBatch"].AsBsonArray.Select(v => v.AsBsonDocument);
128+
var ns = cursorDocument["ns"].ToString();
129+
using (var enumerator = new CursorEnumerator<BsonDocument>(
130+
new ServerInstanceConnectionProvider(connection.ServerInstance),
131+
ns,
132+
firstBatch,
133+
cursorId,
134+
0,
135+
0,
136+
_readerSettings,
137+
BsonDocumentSerializer.Instance,
138+
null))
139+
{
140+
var collections = new List<BsonDocument>();
141+
while (enumerator.MoveNext())
142+
{
143+
collections.Add(enumerator.Current);
144+
}
145+
return collections;
146+
}
122147
}
123148

124149
private IEnumerable<BsonDocument> ExecuteUsingQuery(MongoConnection connection)

0 commit comments

Comments
 (0)