Skip to content

Commit db57f67

Browse files
caosiyangShaneHarvey
authored andcommitted
PYTHON-1371 - The tailable cursor cannot get document through __getitem__(index) on MongoDB v3.4
Fix issue and add test case.
1 parent 74605b7 commit db57f67

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

doc/contributors.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,4 @@ The following is a list of people who have contributed to
7979
- Len Buckens (buckensl)
8080
- ultrabug
8181
- Shane Harvey (ShaneHarvey)
82+
- Cao Siyang (caosiyang)

pymongo/cursor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ def __getitem__(self, index):
590590
clone = self.clone()
591591
clone.skip(index + self.__skip)
592592
clone.limit(-1) # use a hard limit
593+
clone.__query_flags &= ~CursorType.TAILABLE_AWAIT # PYTHON-1371
593594
for doc in clone:
594595
return doc
595596
raise IndexError("no such item for Cursor instance")

test/test_cursor.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,26 @@ def test_tailable(self):
11331133

11341134
self.assertEqual(3, db.test.count())
11351135

1136+
# __getitem__(index)
1137+
for cursor in (db.test.find(cursor_type=CursorType.TAILABLE),
1138+
db.test.find(cursor_type=CursorType.TAILABLE_AWAIT)):
1139+
self.assertEqual(4, cursor[0]["x"])
1140+
self.assertEqual(5, cursor[1]["x"])
1141+
self.assertEqual(6, cursor[2]["x"])
1142+
1143+
cursor.rewind()
1144+
self.assertEqual([4], [doc["x"] for doc in cursor[0:1]])
1145+
cursor.rewind()
1146+
self.assertEqual([5], [doc["x"] for doc in cursor[1:2]])
1147+
cursor.rewind()
1148+
self.assertEqual([6], [doc["x"] for doc in cursor[2:3]])
1149+
cursor.rewind()
1150+
self.assertEqual([4, 5], [doc["x"] for doc in cursor[0:2]])
1151+
cursor.rewind()
1152+
self.assertEqual([5, 6], [doc["x"] for doc in cursor[1:3]])
1153+
cursor.rewind()
1154+
self.assertEqual([4, 5, 6], [doc["x"] for doc in cursor[0:3]])
1155+
11361156
def test_distinct(self):
11371157
self.db.drop_collection("test")
11381158

0 commit comments

Comments
 (0)