application.yml data: mongodb: uri: mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/fecg_dev?slaveOk=true&replicaSet=rs0&write=1&readPreference=secondaryPreferred&connectTimeoutMS=300000
权限配置比较麻烦,演示先不配置
@Test void MongoTest() throws InterruptedException { Param param = new Param(); param.setOrderNo("VipSoft"); List<UserData> list = new ArrayList<>(); for (int i = 0; i < 3; i++) { UserData userData = new UserData(); userData.setDataTime(System.currentTimeMillis()); userData.setDataValue(i); userData.setCreateTime(new Date()); list.add(userData); } param.setUserDataList(list); mongoTemplate.insert(param.getUserDataList(), StrUtil.format("{}", param.getOrderNo())); Set<String> collectionList = mongoTemplate.getCollectionNames(); for (String name : collectionList) { logger.info(name); } }