1.技术点
(1)MongoDB没有显示的创建数据库方式,相反会在第一次写数据的时候创建数据库;
(2)当向集合中写入数据的时候,才会创建数据库文件;
2.常用操作命令(可配合Robo GUI使用)
(1)全量查询
db.trained_test_model.find()
说明:trained_test_model为集合名(集合类似于表)
(2)按条件查询——查询requestTime小于等于('$lte')1533628848197的所有记录
db.trained_test_model.find({'requestTime':{'$lte':"1533628848197"}})
(3)按条件查询——查询trace_id在某些值组成的集合内('$in')的所有记录
db.trained_test_model.find({trace_id:{'$in':["dc5c114ff3854ed9a7291b3f7257dc37","fe8f81959f3dcb5d99891ef1f1012e0e","341dea5c90ed4b8057af8e5515d86cc5"]}})
[if !supportLists](4)[endif]创建集合
db.createCollection(“trained_test_model”)
[if !supportLists](5)[endif]删除集合
trained_test_model.drop
[if !supportLists](6)[endif]删除数据库
db.drop
[if !supportLists](7)[endif]按条件查询并且限定返回条数
db.trained_test_model.find({'request_type':'GET'}).limit(3)
3.JAVA代码配置:
SpringBoot工程,application.properties配置文件中如下配置:
# MongoDB databse
spring.data.mongodb.uri=mongodb://user1:passwd1@10.120.240.211:3119/test
用户名:user1
密码:passwd1
库名:test
4.数据模型实现方法:
使用@Document(collection=“trained_test_model”)注解标明class为trained_test_model集合的数据类;
使用@Field(“interface_name”)注解标明字段名
5.服务注入:
@Autowired
private MongoTemplate mongoTemplate;
6.MongoDB增删改查操作(api):
/**
* AnalysisEngineApplication——MongoDBUnitTest
*
* @author ZhangChi
* @date 2018年5月25日---下午5:33:28
* @version 1.0
*/
@SpringBootTest(classes = AnalysisEngineApplication.class)
@RunWith(SpringRunner.class)
public class MongoDBUnitTest {
@Autowired
private MongoTemplate mongoTemplate;
@Test
@Ignore
public void insertTest() {
System.out.println("----------------------" + mongoTemplate.getDb().getName());
System.out.println(mongoTemplate.collectionExists("mock_server"));
MockServer ms = new MockServer();
ms.setId("3");
ms.setHost("10.98.234.171");
ms.setDomainAndInterface("this is just insert test222!");
ms.setPort(8822);
ms.setSystemName("abc");
mongoTemplate.insert(ms);
}
@Test
public void findTest() {
System.out.println("----------------------" + mongoTemplate.getDb().getName());
System.out.println("---------------------- find test ----------------------");
System.out.println(mongoTemplate.collectionExists("mock_server"));
List res = mongoTemplate.findAll(MockServer.class);
for (int i = 0; i < res.size(); i++) {
System.out.println("res: " + res.get(i).toString());
}
}
@Test
public void countTest() {
System.out.println("---------------------- count test ----------------------");
Query query = new Query();
int t = (int) mongoTemplate.count(query, "mock_server");
System.out.println("count: " + t);
}
@Test
public void aggregationTest() {
System.out.println("---------------------- aggregation test ----------------------");
Aggregation aggregation1 = Aggregation.newAggregation(Aggregation.group("port").count().as("total model"));
AggregationResults x = mongoTemplate.aggregate(aggregation1, "mock_server", BasicDBObject.class);
for (Iterator iterator = x.iterator(); iterator.hasNext();) {
DBObject obj = iterator.next();
System.out.println(obj);
}
}
@Test
public void normalFindTest() {
System.out.println("---------------------- normal find test ----------------------");
Query query = new Query();
Criteria criteria = Criteria.where("systemName").is("mocker");
query.addCriteria(criteria);
System.out.println("normal find res : " + mongoTemplate.find(query, MockServer.class));
}
@Test
public void updateTest() {
System.out.println("---------------------- update test ----------------------");
Query query = new Query();
Criteria criteria = Criteria.where("systemName").is("mocker");
query.addCriteria(criteria);
Update update = new Update();
update.set("systemName", "dick");
System.out.println("update res : " + mongoTemplate.updateFirst(query, update, MockServer.class));
}
@Test
@Ignore
public void createCollectionTest() {
System.out.println("---------------------- create collection test ----------------------");
DBCollection dBCollection = mongoTemplate.createCollection("trained_test_model");
System.out.println("dBCollection res1 : " + dBCollection.getFullName());
System.out.println("dBCollection res2 : " + dBCollection.getCount());
System.out.println("dBCollection res3 : " + dBCollection.getOptions());
}
@Test
public void existTest() {
System.out.println("---------------------- exist test ----------------------");
System.out.println("is exist : " + mongoTemplate.collectionExists("trained_test_model"));
}
}
网友评论