def test_count_with_hint(self, test_db, mongo_version):
collection = test_db.test
await collection.insert_many([{'i': 1}, {'i': 2}])
assert 2 == await collection.find().count()
await collection.create_index([('i', 1)])
assert 1 == await collection.find({'i': 1}).hint('_id_').count()
assert 2 == await collection.find().hint('_id_').count()
with pytest.raises(OperationFailure):
await collection.find({'i': 1}).hint('BAD HINT').count()
# Create a sparse index which should have no entries.
await collection.create_index([('x', 1)], sparse=True)
assert 0 == await collection.find({'i': 1}).hint('x_1').count()
assert 0 == await collection.find({'i': 1}).hint([('x', 1)]).count()
if mongo_version.at_least(3, 3, 2):
assert 0 == await collection.find().hint('x_1').count()
assert 0 == await collection.find().hint([('x', 1)]).count()
else:
assert 2 == await collection.find().hint('x_1').count()
assert 2 == await collection.find().hint([('x', 1)]).count()
评论列表
文章目录