MongoDB的适用场景
MongoDB的行业场景
也可以根据下面这个表判断能否使用MongoDB:
应用特性 | Yes/No |
---|---|
应用不需要事务及复杂join支持 | |
新应用,需求会变,数据模型无法确定,想快速迭代开发 | |
应用需要TB甚至 PB 级别数据存储 | |
应用发展迅速,需要能快速水平扩展 | |
应用要求存储的数据不丢失 | |
应用需要99.999%高可用 | |
应用需要大量的地理位置查询、文本查询 |
如果以上条件满足,那么就可以使用MongoDB。
下面将介绍三种方式连接MongoDB。分别是:MogoClient、MongoTemplate、MongoRepository。
1. MongoClient
引入依赖
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.10.1</version>
</dependency>
文档添加
public static void add(){
MongoClient mongoClient=new MongoClient("192.168.56.115",27017);
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("user");
Document document=Document.parse("{name:'lisi',city:'bj',birth_day:new ISODate('2001-08-01'),expectSalary:18000}");
collection.insertOne(document);
mongoClient.close();
}
文档查询
public static void query(){
MongoClient mongoClient=new MongoClient("192.168.56.115",27017);
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("user");
Document sortDocument=new Document();
sortDocument.append("expectSalary",1);
FindIterable<Document> documents = collection.find().sort(sortDocument);
for (Document document:documents){
System.out.println(document);
}
mongoClient.close();
}
public static void query2(){
MongoClient mongoClient=new MongoClient("192.168.56.115",27017);
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection<Document> collection = database.getCollection("user");
FindIterable<Document> documents = collection.find(Filters.gt("expectSalary", 21000));
for (Document document:documents){
System.out.println(document);
}
mongoClient.close();
}
2. MongoTemplate
2.1 在Spring应用里配置如下:
引入依赖:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>2.0.9.RELEASE</version>
</dependency>
注入Spring Bean
<!-- 构建MongoDb工厂对象 -->
<mongo:db-factory id="mongoDbFactory"
client-uri="mongodb://192.168.211.133:37017/lg_resume">
</mongo:db-factory>
<!-- 构建 MongoTemplate 类型的对象 -->
<bean id="mongoTemplate"
class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg index="0" ref="mongoDbFactory"></constructor-arg>
</bean>
2.2 Spring Boot里配置如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
配置文件:
spring.data.mongodb.host=192.168.56.115
spring.data.mongodb.port=27017
spring.data.mongodb.database=test
开始使用mongoTemplate
@Repository
public class UserDaoImpl implements UserDao{
@Autowired
private MongoTemplate mongoTemplate;
@Override
public List<User> getUser(String name) {
Query query=new Query();
query.addCriteria(Criteria.where("name").is(name));
List<User> users = mongoTemplate.find(query, User.class, "user");
return users;
}
@Override
public User addUser(User user) {
User result = mongoTemplate.insert(user);
return result;
}
@Override
public List<User> getUser(String name, Double expectSalary) {
Query query=new Query();
query.addCriteria(Criteria.where("name").is(name).andOperator(Criteria.where("expectSalary").gt(expectSalary)));
return mongoTemplate.find(query,User.class);
}
@Override
public void updateUser(String name, Double expectSalary) {
Query query=new Query();
query.addCriteria(Criteria.where("name").is(name));
Update update=new Update();
update.set("expectSalary",expectSalary);
mongoTemplate.updateFirst(query,update,User.class);
}
@Override
public void deleteUser(String name) {
mongoTemplate.remove(Query.query(Criteria.where("name").is(name)),User.class);
}
}
3. MongoRepository
3.1 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
3.2 配置文件:
spring.data.mongodb.host=192.168.56.115
spring.data.mongodb.port=27017
spring.data.mongodb.database=test
3.3 编写实体类,并加上@Document("集合名")
3.4 编写Repository接口,继承MongoRepository
public interface UserRepository extends MongoRepository<User, String> {
List<User> findByName(String username);
List<User> findByNameAndExpectSalaryGreaterThan(String username, Double expectSalary);
void deleteByName(String username);
}
3.5 使用Repository
@Repository
public class UserDaoImpl2 implements UserDao{
@Autowired
private UserRepository userRepository;
@Override
public List<User> getUser(String name) {
List<User> users = userRepository.findByName(name);
return users;
}
@Override
public User addUser(User user) {
User result = userRepository.save(user);
return result;
}
@Override
public List<User> getUser(String name, Double expectSalary) {
List<User> users = userRepository.findByNameAndExpectSalaryGreaterThan(name, expectSalary);
return users;
}
@Override
public void updateUser(String name, Double expectSalary) {
User user=new User();
user.setName(name);
user.setExpectSalary(expectSalary);
userRepository.save(user);
}
@Override
public void deleteUser(String name) {
userRepository.deleteByName(name);
}
}
书山有路勤为径,学海无涯苦作舟
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.cnblogs.com/javammc/p/16485991.html
内容来源于网络,如有侵权,请联系作者删除!