docker pull mongo:latest
docker run -d --restart=always -p 27017:27017 --name mymongo -v /data/db:/data/db -d mongo
MongoDB 6.0 及以上版本进入容器使用如下命令
docker exec -it mymongo[创建时对应的MongoDB容器名] mongosh admin
db.createUser({ user:'root',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},'readWriteAnyDatabase']});
【user:‘root’ 】:设置用户名为root
【pwd:‘123456’】:设置密码为123456
【role:‘userAdminAnyDatabase’】:只在admin数据库中可用,赋予用户所有数据库的userAdmin权限
【db: ‘admin’】:可操作的数据库
【‘readWriteAnyDatabase’】:赋予用户读写权限
db.auth('root', '123456')
db.user.insert({"name":"金智秀","age":18})
show dbs
firewall-cmd --add-port=27017/tcp --permanent
firewall-cmd --reload
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-mongodbartifactId>
dependency>
spring:
data:
mongodb:
uri: mongodb://192.168.239.140:27017/test
username: root
password: 123456
@Data
@Document("User")
public class User {
@Id
private String id;
private String name;
private Integer age;
private String email;
private String createDate;
}
@Autowired
private MongoTemplate mongoTemplate;
//添加
@Test
public void createUser() {
User user = new User();
user.setAge(20);
user.setName("金智秀");
user.setEmail("[email protected]");
User user1 = mongoTemplate.insert(user);
System.out.println(user1);
}
//查询所有
@Test
public void findUser() {
List<User> userList = mongoTemplate.findAll(User.class);
System.out.println(userList);
}
//根据id查询
@Test
public void getById() {
User user =
mongoTemplate.findById("64846c08a263b2302594b596", User.class);
System.out.println(user);
}
//条件查询
@Test
public void findUserList() {
Query query = new Query(Criteria
.where("name").is("test")
.and("age").is(20));
List<User> userList = mongoTemplate.find(query, User.class);
System.out.println(userList);
}
//模糊查询
@Test
public void findUsersLikeName() {
String name = "est";
String regex = String.format("%s%s%s", "^.*", name, ".*$");
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Query query = new Query(Criteria.where("name").regex(pattern));
List<User> userList = mongoTemplate.find(query, User.class);
System.out.println(userList);
}
//分页查询
@Test
public void findUsersPage() {
String name = "est";
int pageNo = 1;
int pageSize = 10;
Query query = new Query();
String regex = String.format("%s%s%s", "^.*", name, ".*$");
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
query.addCriteria(Criteria.where("name").regex(pattern));
int totalCount = (int) mongoTemplate.count(query, User.class);
List<User> userList = mongoTemplate.find(query.skip((pageNo - 1) * pageSize).limit(pageSize), User.class);
Map<String, Object> pageMap = new HashMap<>();
pageMap.put("list", userList);
pageMap.put("totalCount",totalCount);
System.out.println(pageMap);
}
//修改
@Test
public void updateUser() {
User user = mongoTemplate.findById("64846c08a263b2302594b596", User.class);
user.setName("test_1");
user.setAge(25);
user.setEmail("[email protected]");
Query query = new Query(Criteria.where("_id").is(user.getId()));
Update update = new Update();
update.set("name", user.getName());
update.set("age", user.getAge());
update.set("email", user.getEmail());
UpdateResult result = mongoTemplate.upsert(query, update, User.class);
long count = result.getModifiedCount();
System.out.println(count);
}
//删除操作
@Test
public void delete() {
Query query =
new Query(Criteria.where("_id").is("64846c08a263b2302594b596"));
DeleteResult result = mongoTemplate.remove(query, User.class);
long count = result.getDeletedCount();
System.out.println(count);
}