- mongoTemplate 获取 collection ,collection 是实时更新的
@SpringBootTest
class DemoApplicationTests {
@Autowired
private MongoTemplate mongoTemplate;
@Test
void test1() throws InterruptedException {
while (true){
Thread.sleep(1000);
mongoTemplate.insert(new A());
}
}
@Test
void test2() throws InterruptedException {
MongoCollection<Document> test = mongoTemplate.getCollection("test");
while (true){
Thread.sleep(1000);
System.out.println(test.countDocuments());
}
}
}